Je suis novice en programmation multi-thread et j'ai une question à propos de pthreads.comportement de pthreads et de la fonction pthread_join
Ceci est le code de test que je cours et je ne comprends pas son comportement. Quelqu'un peut-il jeter un peu de lumière s'il vous plaît.
void *t1(void *args){
printf("returning from t1\n");
return;
}
void *t2(void *args){
printf("returning from t2\n");
return;
}
int main(){
pthread_t thread1,thread2;
int r1,r2;
r1=pthread_create(&thread1,NULL,t1,NULL);
r2=pthread_create(&thread2,NULL,t2,NULL);
pthread_join(thread1,NULL);
// pthread_join(thread2,NULL);
return 0;
}
Le comportement de ce programme est l'une des 5 indiqués ci-dessous
[email protected]:FFTW$ ./ptest
returning from t2
returning from t1
[email protected]:FFTW$ ./ptest
returning from t1
returning from t2
[email protected]:FFTW$ ./ptest
returning from t1
[email protected]:FFTW$ ./ptest
returning from t2
returning from t2
[email protected]:FFTW$ ./ptest
returning from t1
returning from t2
returning from t2
Je ne comprends pas la 4ème et la 5ème sortie. Pourquoi le thread t2 s'exécute-t-il deux fois? Bien sûr, si je décommente pthread_join(&thread2,NULL,t2,NULL)
le programme se comportera correctement mais je suis particulièrement intéressé par le cas où un seul thread rejoint le thread principal().
grâce, Mir
Il nous manque du code? Vous avez "free (a)" et a ne semble pas être déclaré. – bramp
ooops! Pardon! C'était une faute de frappe. Je l'ai corrigé. La question reste la même. – Mir