gpt4 book ai didi

c++ - 免费互斥问题

转载 作者:行者123 更新时间:2023-11-28 08:20:57 25 4
gpt4 key购买 nike

大家好,对于我遇到的一个小问题,如果能提供任何帮助,我们将不胜感激。基本上我的程序运行良好,但没有通过所有必需的测试。显然问题不在于执行以下操作之一:

  • 您加入了您创建的所有线程
  • 你销毁所有你初始化的mutices
  • 你解锁所有你锁定的mutices

谁能看出我哪里出错了?非常感谢任何帮助,谢谢

   #include "counter.h"

/* ============================================================================
* File-global variables
* ========================================================================== */
static int ncounters = 0;
static struct counter *counters = NULL;

static int nthreads = 0;
static int *ninstructions = NULL;
static struct instruction **instructions = NULL;


/* ============================================================================
* Operations
* ========================================================================== */
static void
decrement(long long *n) {
*n = *n-1;
}

static void
increment(long long *n) {
*n = *n+1;
}

static void
mult2(long long *n) {
long long s = 2;
long long t = *n;
long long q = t*s;
*n = q;
}


/* ============================================================================
* Helper functions
* ========================================================================== */

int
quit(void) {
int i;
for (i=0; i<nthreads; ++i) {
free (instructions[i]);
}
free (instructions);
for (i=0; i<ncounters; ++i) {
pthread_mutex_t *my = &(counters[i].mylock);
pthread_mutex_destroy(my);
}
free (counters);
free (ninstructions);
return 0;
}

/* ============================================================================
* Thread function
* ========================================================================== */
static void *
worker_thread(void *arg) {
int t = *((int*)arg);
int l;
for (l = 0; l<ninstructions[t]; ++l) {
int y;
struct instruction* curr = &instructions[t][l];
pthread_mutex_lock(&curr->counter->mylock);
for (y=0; y<curr->repetitions; ++y) {
long long *g = &curr->counter->counter;
(curr->work_fn)(g);
}
pthread_mutex_unlock(&curr->counter->mylock);
}

return NULL;
}


/* ============================================================================
* Main function
* ========================================================================== */
int
main(void) {
if (scanf("%d", &ncounters) != 1 || ncounters < 1) {
printf("error\n");
return quit();
}
counters = (struct counter*)malloc(ncounters*sizeof(struct counter));


if (scanf(" %d", &nthreads) != 1 || nthreads < 1) {
printf("error\n");
return quit();
}
ninstructions = (int *)malloc(nthreads*sizeof(int));
instructions = (struct instruction**)malloc(nthreads*sizeof(struct instruction*));
int i;
for (i=0; i<nthreads; ++i) {

if (scanf(" %d", &ninstructions[i]) != 1) {
printf("error\n");
return quit();
}
instructions[i] = (struct instruction*)malloc(ninstructions[i]*sizeof(struct instruction));
int k;
for (k=0; k<ninstructions[i]; ++k) {
int c, r;
char f;
if (scanf(" %d %c %d", &c, &f, &r) != 3 || c>ncounters-1) {
printf("error\n");
return quit();
}
struct instruction* curr = &instructions[i][k];
struct counter* currcp = &counters[c];
pthread_mutex_init (&currcp->mylock, NULL);
curr->counter = currcp;
curr->repetitions = r;
switch(f) {
case 'I':
curr->work_fn = increment;
break;
case 'D':
curr->work_fn = decrement;
break;
case '2':
curr->work_fn = mult2;
break;
default:
printf("error\n");
return quit();
}
}
}
int w;
pthread_t threadIDs[nthreads];
int args[nthreads];
for (w=0; w<nthreads; ++w) {
args[w] = w;
pthread_create(&threadIDs[w], NULL, worker_thread, (void *) &args[w]);
}
int u;
for (u=0; u<nthreads; ++u) {
pthread_join(threadIDs[u], NULL);
}
int d;
for (d=0; d<ncounters; ++d) {
printf("%lld\n", counters[d].counter);
}
return quit();
}

和数据结构

#ifndef __COUNTER_H__
#define __COUNTER_H__

#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>


/* counter data structure */
struct counter {
pthread_mutex_t mylock;
long long counter; /* to store counter */
};

/* counter value */
struct instruction {
struct counter *counter; /* pointer to counter */
int repetitions; /* number of repetitions */
void (*work_fn)(long long *); /* function pointer to work function */
};

#endif

是的,抱歉,我认为没有格式可能是可能的:

<number of counters>
<number of threads>
<instruction-sequence>
<instruction-sequence>
....
<number of instructions>
<instruction>
<instruction>
....

因此对于每个指令序列,您都有一个指令列表,这些指令由每个线程在一个或多个计数器上执行。即输入:

2
2
1
0 I 10
2
1 D 10
1 2 2

将产生结果:

10
-40

(只有三种指令类型:自增(I),自减(D),乘以2(2)其中指令的格式为:

<counter> <function> <repitition>

这有道理吗?

最佳答案

好吧,看起来您正在加入您创建的所有线程,并解锁您锁定的所有 mutices —— 这部分相当简单。

然而,从第二个条件来看,mutices 的创建和销毁之间似乎没有明显的相关性。

每条指令调用一次 pthread_mutex_init,而每条计数器调用一次 pthread_mutex_destroy。我看不到指令数等于计数器数的任何保证。

我想您希望每个计数器有一个互斥量。因此,为每条指令初始化一个互斥锁是没有意义的。您可能需要一个初始化例程,它为每个计数器初始化一个互斥锁,以反射(reflect)您的 quit 例程,该例程会破坏每个计数器的互斥锁。

关于c++ - 免费互斥问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5856055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com