이 pthreads에 익숙하지 않습니다. 숫자를 무작위로 표시하는 대신 순서대로 표시해야하는 프로그램을 작성했습니다. 나는 pthrea_join() 방법을 사용했다.pthread가 순차적으로 실행되지 않습니다.
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을 얻고 다음과 같이 프로그램입니다. help
전역 변수'i'에 액세스 할 때 문제가있는 것 같습니다. 주 스레드 (증가)뿐만 아니라 작업 스레드에서. 그 외에는 무엇을 기대합니까? 스스로 기다려? – Xyand
죄송합니다. 편집하는 것을 잊어 버렸습니다 ... 지금도 숫자가 무작위로 나타납니다. – nitish712