4

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?

Translucent Pain
  • 1,441
  • 2
  • 14
  • 18
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356

1 Answers1

4

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.

Duck
  • 26,924
  • 5
  • 64
  • 92
  • Threads cannot be killed. They can be cancelled, but they could install a cleanup handler that waits on the barrier, if necessary. – R.. GitHub STOP HELPING ICE Jul 05 '11 at 02:33
  • 2
    Killed might have been ambiguous. Let's settle on somehow sent a signal to shutdown without waiting on the barrier and leaving the other threads hanging. Cleanup handler are definitely an alternative way of cleaning up. – Duck Jul 06 '11 at 00:35