Condition variables?

David Brown dlang at davidb.org
Tue Oct 2 10:32:23 PDT 2007


On Tue, Oct 02, 2007 at 09:52:35AM -0700, Sean Kelly wrote:
> Janice Caron wrote:
>> On 10/2/07, David Brown <dlang at davidb.org> wrote:
>>> This feature is the very magic about condition variables that makes race
>>> free synchronization possible where it isn't with events.
>> Now that part is not right. Race-free synchronization is /always/
>> possible with events. (Note, however, that I say "possible", not
>> "guaranteed".
>
> How so?  Win32 events are widely regarded as broken on 
> comp.programming.threads specifically because of race issues with them.  
> Personally, I think they're fine with only one consumer, but things get a 
> bit odd with multiple consumers.

It's certainly possible, but it requires a possibly unbounded loop to
detect and recover from the race case.  It would only loop in the unlikely
even of the exact timing needed to make the race happen each iteration.

The real problem is that the obvious way of doing things is the way that
has the race, and then it gets missed and things deadlock.

Condition variables can have starvation problems with multiple consumers.
Generally, with multiple consumers, it is best to build a higher-level
abstraction that can be reasoned through for correctness and fairness, and
then used.

Dave



More information about the Digitalmars-d mailing list