【发布时间】:2016-09-10 00:10:05
【问题描述】:
我对多线程很陌生,我试图在不使用全局变量的情况下增加一个共享计数器,我的目标是尝试最大化不同线程之间的并发性并增加变量,直到我在参数中给出一个数字......对不起,如果是一个蹩脚的问题,但我想在这里得到帮助,当我编译我的代码并运行它时,我得到一个分段错误......我认为错误出现在我创建的变量计数和共享计数器中!
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
typedef struct {
long *cnt; /* pointer to shared counter */
long n; /* no of times to increment */
int nr;
pthread_t id; /* application-specific thread-id */
} targ_t;
void *sfun(void *arg) {
targ_t *est = (targ_t *) arg;
here:
pthread_mutex_lock(&mutex);
(*(est->cnt))++;
pthread_mutex_unlock(&mutex);
if(*(est->cnt)<est->n)
goto here;
return NULL;
}
int main(int argc, char *argv[]){
targ_t real[3];
int c=0;
long count=0;
real[0].cnt=&count;
pthread_mutex_init(&mutex, NULL);
for(c=0;c<3;c++){
real[c].n=atoi(argv[1]);
real[c].nr=c+1;
pthread_create(&real[c].id,NULL,&sfun,&real[c]);
}
for(c=0;c<3;c++){
pthread_join(real[c].id,NULL);
}
pthread_mutex_destroy(&mutex);
printf("OVERALL %lu\n", count);
return 0;
}
提前 TY。
【问题讨论】:
-
实现“共享计数器”的常用方法是将计数器作为参数传递给 pthread_create(即第二个参数)。然后您可以使用原子添加操作来增加计数器。
-
用
here:和gote here;标签写出循环并不是一个特别好的主意。在某些场合(一些但不是很多场合)适合使用goto— 这不是少数场合之一。 -
您实际上并没有验证您的代码是否获得了
argv[1]来转换;会不会是你忘了传递那个论点? -
然而,你的主要问题是你初始化了
real[0].cnt,但你没有初始化real[1].cnt或real[2].cnt,所以那些线程正在访问谁知道什么内存——可能是他们使用空指针,或者它可能是指向内存中任何位置的指针,无论分配与否,可写与否。你也错过了<stdlib.h>。 -
你能举个例子吗? @bruceg
标签: c multithreading struct counter