What sync object should i use?
Dmitry Olshansky
dmitry.olsh at gmail.com
Tue May 14 09:24:34 PDT 2013
14-May-2013 20:09, Heinz пишет:
> On Monday, 13 May 2013 at 21:04:23 UTC, Juan Manuel Cabo wrote:
>
>> There is one thing that should definitely added to the documentation,
>> and that is what happens when one issues a notify while the thread
>> hasn't yet called Condition.wait().
>
> I can confirm that under Win32 calling notify() before wait()
> internally signals the condition and then calling wait() returns
> immediately and actually does not wait. This is the expected
> behavior and is actually how Win32 events work.
> On Tuesday, 14 May 2013 at 08:58:31 UTC, Dmitry Olshansky wrote:
>
>> Have to lock it otherwise you have a race condition on a condition
>> variable (wow!).
>
> Ok, i'll lock it just in case. It also makes me feel my code is
> more robust. This will do right?
>
> ...
> synchronized(cond.mutex)
> cond.notify();
> ...
>
> My internal bool variable that affects the condition (the one
> that decides if the consumer thread should wait) must be setable
> at any moment by any thread so i leave it outside the lock.
Wrong. Before setting it from some other thread you should grab the
lock. See Sean's example.
> Also,
> after setting this variable i immediately call notify() with
> mutex unlocked. That's why it is working i think.
>
--
Dmitry Olshansky
More information about the Digitalmars-d-learn
mailing list