【发布时间】:2010-04-05 01:45:11
【问题描述】:
我有这段代码给我带来了麻烦。 我知道所有线程都在读取相同的结构。但我不知道如何解决这个问题。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int a,b;
} s_param;
void *
threadfunc(void *parm)
{
s_param *param2 = parm;
printf("ID:%d and v:%d\n",param2->a,param2->b);
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
pthread_t thread[3];
int rc=0,i;
void * status;
for(i=0; i<3 ; ++i){
s_param param;
param.b=10;
param.a=i;
rc = pthread_create(&thread[i], NULL, threadfunc, ¶m ); // !!!!
if(rc){
exit(1);
}
}
for(i=0; i<3 ; ++i){
pthread_join(thread[i],&status);
}
return 0;
}
输出:
ID:2 and v:10
ID:2 and v:10
ID:2 and v:10
我需要什么:
ID:0 and v:10
ID:1 and v:10
ID:2 and v:10
【问题讨论】:
标签: c parameters struct pthreads