What sync object should i use?
Sean Kelly
sean at invisibleduck.org
Tue May 14 12:19:29 PDT 2013
On May 14, 2013, at 12:02 PM, Dmitry Olshansky <dmitry.olsh at gmail.com> wrote:
> 14-May-2013 21:02, Steven Schveighoffer пишет:
>>
>> But since you have to lock anyway, signaling while holding the lock, or
>> while being outside the lock isn't really a difference.
>>
>
> On the level of gut feeling there must be something about it as you don't see:
>
> synchronized(m){
> // ... send message
> }
> notify();
>
> anytime of day. And hosting work out of mutex seems natural isn't it?
If you move the notify out of the mutex then you can end up with multiple threads competing for the same value. Say the producer puts a value in the queue, leaves the mutex, and notifies a waiting thread. Then consumer A enters the mutex, sees that there's something in the container and takes it, then consumer B receives the notification and wakes up to discover that the container is empty. So long as your wait loops are done properly the only bad result will be pointless wakeups, but worst case you could have a crash or exception if you're removing data from the container without checking if it's empty.
More information about the Digitalmars-d-learn
mailing list