What is a barrier?

Ensure N tasks reached a certain point in code before proceeding.

This synchronization mechanism is known as a barrier.

Interface

// some code
Barrier(N); (N-1) tasks are blocked
// Continue after N processes

implementation

int arrived = 0; // shared variable
mutex = S(1); // binary semaphore to provide mutual exclusion
waitQ = S(0); // for N - 1 processes to block

Barrier(N) {
  wait(mutex);
  arrived++; // shared resource
  signal(mutex);
  
  if (arrived == N) {
    signal(waitQ) // Nth process wakes up a sleeping process
  }
  
  wait(waitQ); // All processes sleep; once we hit the Nth process,
  signal(waitQ); // they wake up and propagate through to wake all processes up
}