About user pthread_barrier_wait

I use pthread_barrier_wait to synchronize threads, but in my program there is a chance that one or more threads will expire while others are waiting for them to reach pthread_barrier_wait. Now is there a way that threads stuck in pthread_barrier_wait know that some of the threads expired before everyone reached the barrier?

+3
source share
1 answer

It 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 is a programmed expiration, then just call wait on it at that point. The barrier count is reduced, and when the threads are freed, you can perform a normal error check, and then immediately call pthread_exit or something else. Including pthread_wait in a separate function can simplify the situation.

if (must_die)
{
    do_barrier_wait();
    pthread_exit(NULL);
}   

If the threads expire because they are killed or canceled, life becomes more complicated, and you are probably heading to the monumental territory of hacking, and perhaps you should reconsider the design.

+4
source

All Articles