【发布时间】:2012-08-11 16:17:44
【问题描述】:
我是这些 pthread 的新手。我编写了一个程序,以便按顺序显示,而不是随机显示数字。我为此使用了 pthrea_join() 方法。程序如下:
int cnt=0,i=1;
pthread_t th[10];
int printmsg()
{
cnt++;
printf( "thread no. %d\n",cnt);
}
void tosync()
{
if(i>0)
pthread_join(th[i-1],NULL); // <---here i am blocking the previous thread..
printmsg();
}
void main(void)
{
pthread_create(&th[0], NULL,(void*)&tosync, NULL);
for( i=1;i<10; i++){
pthread_create(&th[i],NULL, (void*) &tosync, NULL);
}
int y;
for(int i=0; i<10; i++)
pthread_join(th[i],NULL);
return;
}
我仍然随机获取数字... 请。帮助
【问题讨论】:
-
访问全局变量
i时好像有问题。来自主线程(增量)以及来自工作线程。除此之外,您希望th[0]做什么?等待自己? -
对不起,我忘了编辑...即使现在我还是随机获取数字
标签: c synchronization pthreads