@mashingan, interesting example. The consumer pattern is the classic use case 
for a condition variable.

> From what I remembered, signaling condition before the condition waiting will 
> make the signal lost.

We agree on this point. The signal will be lost if you signal before the wait.

> Also, signaling no matter how many are done, condition will only be handled 
> once.

this is not correct. You only observe this because you only have one consumer 
thread.

If you had multiple consumer threads waiting on the condition, then each thread 
would need to be signaled.

The signal would not be "handled only once" as you say. It would be handled 
once for each waiting thread. (Or a broadcast call would need to be made to 
wake all waiting threads at once).

The canonical use case for a condition variable is single producer, multiple 
consumer. So multiple signals must be handled.

* * *

One other small thing is that it looks like your sample code has the same bug 
as @mikra, you do not acquire the lock before waiting on the condition in your 
_chanWaiting()_ thread.

This may or may not affect anything, it depends on the underlying OS 
implementation of condition variable, but it's a good idea in order to prevent 
weird behavior (such as lost signals).

Reply via email to