我是这些pthreads的新手。我写了一个程序,以便随机显示数字,而不是按顺序显示。我已经使用pthrea_join()方法。该程序如下:pthreads不是连续执行的
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;
}
不过我得到随机数字...... plzz。帮助
看来你在访问全局变量'i'时有问题。从主线程(增量)以及工作线程。除此之外,你期望'th [0]'做什么?等待自己? – Xyand 2012-08-11 18:02:25
对不起,我忘了编辑...即使现在我随机得到的数字 – nitish712 2012-08-12 07:51:03