Abstract


  • Synchronising Thread while they are running concurrently. So that they all reach the same point before any of them proceed
  • Used when we need information from multiple Thread at a particular execution point. For example Roll Dice
  • Need to compile with -pthread flag

Code Snippet


#include <pthread.h>
 
int main(int argc, char *argv[]) {
  pthread_barrier_t barrier;
  
  // A barrier with one thread. Synchornising 3 threads
  pthread_barrier_init(&barrier, NULL, 3);
 
  // Denote it is ready to be synchornised with other threads
  pthread_barrier_wait(&barrier);
 
  pthread_barrier_destroy(&barrier);
  return 0;
}

Example


Roll Dice

  • Each Thread rolls a dice, the main thread decides who is the winner. Then each thread shouts if it is a winner or not
  • Sample Code