Array of semaphores N-1 and mutex? All threads acquire a mutex, including a counter, and if less than N, free the mutex and expect a semaphore array on [counter]. The nth thread finds the counter equal to N, signals all semaphores, resets the counter to 0, executes "B", releases the mutex and outputs. Other threads, when released, also execute B, but cannot loop and re-enter until the Nth thread executes "B" and releases the mutex.
All multi-tasking OS have semaphores / mutexes. You can use an event, if available, instead of a semaphore.
Martin james
source share