c++ - behaviour of pthreads and pthread_join function -
i new multi-threaded programming , have question pthreads.
this test code run , don't understand behaviour. can throw light on please.
void *t1(void *args){       printf("returning t1\n");       return;   }    void *t2(void *args){       printf("returning 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;   }     the behaviour of program either of 5 shown below
murtuza@murtuza:fftw$ ./ptest   returning t2   returning t1   murtuza@murtuza:fftw$ ./ptest   returning t1   returning t2   murtuza@murtuza:fftw$ ./ptest   returning t1   murtuza@murtuza:fftw$ ./ptest   returning t2   returning t2   murtuza@murtuza:fftw$ ./ptest   returning t1   returning t2   returning t2     i don't understand 4th , 5th output. why thread t2 executing twice? of course, if uncomment pthread_join(&thread2,null,t2,null) program behave correctly interested in case 1 thread joins main() thread.
thanks, mir
i'm afraid wasn't able replicate problem.
i ran:
#include <pthread.h> #include <stdio.h>  void *t1(void *args){       printf("returning t1\n");       return null;   }    void *t2(void *args){       printf("returning t2\n");       return null;   }    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;   }     as:
while (true) ; ./ptest ; date ; done   and spotted: t1,t2 ; t2,t1 , t1.
but never repeated entries, or missing t1.
sorry.
maybe there's broken in threading library, or in printing out threaded process?
Comments
Post a Comment