The implementation of condition variables, on linux at least, is pthreads. But 
the implementation is tangential in this case.

I think this example would deadlock in C++ as well.

You signal() all the threads in the main thread only once, and then wait for 
them.

It's possible that the thread scheduler will context switch to one of the other 
threads between each iteration of the signal() loop, in which case your code 
would work, but that is unlikely.

More likely, One thread will win the lock from that signal() loop, and the rest 
will go back to sleep. After that initial loop, there is no mechanism to wake 
the threads up again.

You need to either put a signal() call in threadFunc (cooperative locking), or 
have the main thread continue calling signal() instead of waiting.

* * *

note: that the semantics of signal() are to wake a single thread waiting on the 
conditional (notify_one in std::condtion_variable or pthread_cond_signal in 
pthreads)

It is _not_ broadcast semantics. I.E. wakes all threads (notify_all in 
std::condition_variable or pthread_cond_broadcast in pthreads).

Though it might be a nice to add that api ... hint to @araq and core devs lol.

Reply via email to