c - What if thread exit before other thread wait for it(join)? -


for example, if create 3 threads , join them in same order. if second thread exit first, happen pthread_join. program block until tid1 exits or directly return pthread_join(&tid2,null)?

   pthread_t tid1,tid2,tid3;    pthread_create(&tid1, null, somefun, null);    pthread_create(&tid2, null, somefun, null);    pthread_create(&tid3, null, somefun, null);    pthread_join(&tid1, null);    pthread_join(&tid2, null);    pthread_join(&tid3, null); 

when code calls:

pthread_join(&tid1, null); 

if tid1 has not exited yet, call block until does. if tid2 exits in meantime, doesn't change behavior of particular call. in scenario, when call return, next call:

pthread_join(&tid2, null); 

will return since tid2 has exited.

if want perform work when arbitrary thread finished, you'll need use other pthread_join() synchronize "some thread finished" event. perhaps waiting on condition variable gets signaled every thread when complete (along mechanism such queue waiting thread can determine thread has signaled completion). mechanism used have threads write information pipe main (or controlling) thread reads notification.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -