Reputation: 30765
I'm using pthread_barrier_wait to synchronize threads, but in my program there is a possibility that one or more of the threads expire while others are waiting for them to reach pthread_barrier_wait. Now is there a way, that threads stuck at pthread_barrier_wait know that some of the threads have expired while all have reached the barrier?
Upvotes: 4
Views: 2017
Reputation: 27552
That depends a lot on how and why they expire.
The barrier doesn't care where pthread_barrier_wait() is called on it so if it's a programmed expiration then just call wait on it at that point. The barrier counter gets decremented and when the threads are released you can do the normal error checking and then just immediately call pthread_exit or whatever. Putting the pthread_wait in a separate function might simplify things.
if (must_die)
{
do_barrier_wait();
pthread_exit(NULL);
}
If the threads are expiring because they are getting killed or canceled then life is more complicated and you are probably headed into monumental hack territory and it might be worth reconsidering the design.
Upvotes: 4