Boost logo

Boost Users :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2008-04-08 10:54:35


Anteru <newsgroups_at_[hidden]> writes:

> The documentation of Condition::notify_one says it unblocks one of the
> threads waiting currently on the condition -- what happens if I have
> several threads waiting and I call notify_one twice?

If there are multiple threads waiting, the first call to notify_one() should
wake one, and the second call should wake another.

> It seems that on Windows/x64/VC9, only on thread wakes up if notify_one
> is called twice in a row. Is this right?

That would be incorrect behaviour if there really is two threads waiting.

> Second, wait says: "The thread will unblock when notified by a call to
> this->notify_one() or this->notify_all(), or spuriously". Is it likely
> that a condition unblocks spuriously? After all, I'd expect to unblock
> only after calling notify_* and not "by accident". Is there any way to
> get diagnostic output when a wait has been called spuriously?

It is not possible to detect a spurious wake: if it was, then the
implementation would resume waiting. However, spurious wakes should be rare
occurrences.

> The problem I'm having at the moment is: I have PIMPLed Boost.Thread
> behind some simple wrappers, and I'm using them like this here:
>
> Lock lock (mutex_);
>
> if (queue.empty ())
> {
> waitForItem.wait (lock);
> }
>
> with Lock being a wrapper for scoped_lock, mutex being a wrapper for
> mutex and waitForItem being a condition, and funnily enough it goes
> waiting even though there is an item in queue (i.e., queue.empty () ==
> false). I'm not sure where this is coming from (all access to queue is
> protected using mutexes), only candidate at the moment is that when
> inserting, I call notify_one, and if two items get inserted one right
> after the other, I assume that only one thread is woken up and hence the
> other remains waiting although there is an item in the queue.

Notifies are not sticky, so if there is no thread waiting when notify_one() is
called, the notify is lost.

> It works when I use notify_all (), but this does not smell right to me :/

Can you show me some code?

Anthony

-- 
Anthony Williams            | Just Software Solutions Ltd
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net