병렬 스레드를 만들고 싶습니다. 예 : 내 출력은 같은 것입니다 : thread1 thread3 thread4 thread2 ... 주에서 :스레드가 병렬 실행되지 않음
pthread_t tid;
int n=4;
int i;
for(i=n;i>0;i--){
if(pthread_create(&tid,NULL,thread_routine,&i)){
perror("ERROR");
}
pthread_join(tid,NULL);
}
그리고 내 기능 (루틴)입니다 : thread1 thread2 thread3 :
void *thread_routine(void* args){
pthread_mutex_lock(&some);
int *p=(int*) args;
printf("thread%d ",*p);
pthread_mutex_unlock(&some);
}
난 항상하지 병렬 결과를 얻었다 thread4. 이 스레드를 같은 시간에 병렬로 실행하고 싶습니다. 어쩌면 문제는 위치 pthread_join이지만 어떻게 해결할 수 있습니까?
'& i'을 스레드 인수로 전달하면 스레드가 인쇄하기 전에'i'가 변경 될 수 있으므로 문제가 발생할 수 있습니다. 가장 간단한 수정은 포인터 대신'i' 캐스트를 전달하는 것입니다. (이것은 당신이받은 답안에 이미 언급 된'join()'문제에 추가됩니다) – Dmitri
@Dmitri 네, 여기가 문제입니다. –