// How many threads have arrived to the barrier
shared count := 0
// Protects the increment of the count
shared mutex := semaphore(1)
// Locked (0) until all threads arrive, then it is unlocked (1)
shared barrier := semaphore(0)
main:
// Create arbitrary amount of threads
shared thread_count := read_integer()
create_thread(secondary, thread_count)
secondary:
Statement A
// Barrier: a generalized rendezvous
wait(mutex)
// If this is the last thread that reaches the barrier
count := count + 1
if count = thread_count then
while count > 0 do
signal(barrier)
count := count - 1
signal(mutex);
wait(barrier)
// Statement B must be executed until
// all threads have executed Statement A
Statement B