What sync object should i use?

Steven Schveighoffer schveiguy at yahoo.com
Tue May 14 10:02:31 PDT 2013


On Tue, 14 May 2013 04:58:27 -0400, Dmitry Olshansky  
<dmitry.olsh at gmail.com> wrote:

> 14-May-2013 08:33, Heinz пишет:
>>> BTW, given recent discussion on memory barriers, I think my previous
>>> statement that the mutex does not need to be locked to call notify is
>>> probably incorrect.
>>
>
> Have to lock it otherwise you have a race condition on a condition  
> variable (wow!).

No, the issue would be reordering (is that possible in this case?).  The  
actual signaling of the condition would not require the lock, but you  
still need to lock to send the message (e.g. set the boolean) or you will  
definitely have issues.

But since you have to lock anyway, signaling while holding the lock, or  
while being outside the lock isn't really a difference.

Maybe I'm wrong...

>> Haven't had any issues calling notify outside a synchronized statement,
>> even from multiple threads. At least this works under Win32 with my
>> producer thread all wrapped inside synchronized(). Only a single
>> consumer thread is inside synchronized() but then i have 2 more threads
>> making naked calls to notify(). Not a single crash.
>
> Doesn't prove anything, it could happen that you just miss a  
> notification, for instance. Another common case is that it so happens  
> that wait will (with luck) always happen before any of notify and  
> notifications come spaced out in time.

I don't see how you could miss a notification, can you explain further?

-Steve


More information about the Digitalmars-d-learn mailing list