Instead of turning, use the mutex / condvar pthread primitives. I would suggest one mutex to protect both the number of invisible threads and condvar.
The main loop is as follows:
acquire mutex
count=N_THREADS;
start your N threads
while (1) {
if (count==0) break;
cond_wait(condvar);
}
release mutex
And when each thread is ready, it will do something like this:
acquire mutex
count
cond_signal(condvar)
release mutex
(EDIT: I suggested that threads should continue as soon as they have done their stuff for initialization. If they are finished, use pthread_joinas others have said.)
source
share