我是這些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