是否存在,而在Linux GCC使用pthread.h
保持局部变量线程功能的方法:保持并行线程局部变量
int i = 42; // global instance of i
int main() {
pthread_t threads[2];
long t;
pthread_create(&threads[t], NULL, ThreadFunction, (void *) t;
pthread_create(&threads[t], NULL, ThreadFunction2, (void *) t;
}
我不知道是否有在POSIX函数中的参数创建新的线程并保持局部变量:
void *ThreadFunction(void *threadid)
{
int i=0;
i++; // this is a local instance of i
printf("i is %d", i); // as expected: 1
}
void *ThreadFunction2(void *threadid)
{
i += 3; // another local instance -> problem
}
事后哪里是i
42.就算我已经定义了一个i
以前我想这i
不要被我的线程中。
你的问题是什么?代码中的所有'i's都是局部变量。结果42证实了这一点。 – 2010-11-07 18:04:03
恩,这个例子很糟糕...我明白了。我想我在线程1是1,在线程2是3,并保持那样...必须编辑。 – wishi 2010-11-07 18:09:07
@Duck:你可以修改哪些代码?简单的解决方案是在本地声明第二个'i'。 – kennytm 2010-11-07 18:18:11