- So first all your N worker-threads have to go into "ready mode"
- Your stimulus thread sends the stimulus. (It has to wait until all the others are ready)
- Then all the threads should start working.
So between 1. and 2. you need a semaphore (initial count 0, max count nThreads), which is signalled by each worker thread once, and waited for by the stimulus-thread N times.
Between 2. and 3. you also need a synchronisation, because the worker-threads should wait until the stimulus thread has sent his stimulus. This can also be achieved by a semaphore(initial count 0 max count 1) for each worker-thread.
pseudo-code for stimulus-thread:
init stimulus-semaphore and one semaphore for each worker-thread
start threads
while (1) {
for (int i=0;i<nThreads;i++)
wait for stimulus-semaphore
do the stimulus-thing
for (int i=0;i<nThreads;i++)
signal threads[i] semaphore
}
pseudo code for worker-thread:
while (1) {
signal stimulus-semaphore
wait for this threads semaphore
do the work to be done after stimulus was sent
}