I think you may be right -- that should be while(), not if(). I don't know what I was thinking (probably I was still getting up to speed on the finer points of the boost threading model). Conditions are also not guaranteed to not have spurious wakeups, so you generally need to embed them in a loop that tests the actual thing you're interested in.
As for notify_one() instead of notify_all(), you are probably right, assuming that if notify_one() is called twice on a condition it will always be the case that at least two threads are woken up, not that you wake up the same thread twice. I'll see about making those changes. Thanks! On Sat, Mar 10, 2007 at 05:11:40PM -0800, Ken Taylor wrote: > I'm trying to get familiar with boost threads, and I was going through > vutil's synqueue.hh and something didn't make sense to me... > > let's say that you have an empty queue, and two threads try to call > popOrWait at the same time: > > Element popOrWait() { > boost::mutex::scoped_lock l(lk); > if(thequeue.empty()) cond.wait(l); > Element e = thequeue.front(); > thequeue.pop_front(); > return e; > } > > Ok, so the first one gets the lk mutex, sees the queue is empty, and waits > on the condition, simultaneously releasing the mutex. The second thread then > gets the mutex and also waits on the condition. > > Let's say then a third thread comes along and adds one element to the queue > with a push: > > void push(Element e) { > boost::mutex::scoped_lock l(lk); > thequeue.push_back(e); > cond.notify_all(); > } > > Upon the notify_all(), both the previous threads will wake up and try to > re-acquire the lk lock. One will succeed, pop the value off the queue, and > exit, releasing the lock. Then the other will get the lock and try to pop a > value off a now-empty-again queue! > > This seems like a problem... shouldn't popOrWait be something more like > while(thequeue.empty()) cond.wait(l);? And why use a notify_all rather than > notify_one? (Though some googling has revealed that notify_one might not > guarantee that only one gets notified -- just that at *least* one gets > notified -- at least on a pthreads-based platform. See > http://www.opengroup.org/onlinepubs/009695399/functions/pthread_cond_signal.html > down under "Multiple Awakenings by Condition Signal") > > - Ken > > > _______________________________________________ > vos-d mailing list > vos-d@interreality.org > http://www.interreality.org/cgi-bin/mailman/listinfo/vos-d -- [ Peter Amstutz ][ [EMAIL PROTECTED] ][ [EMAIL PROTECTED] ] [Lead Programmer][Interreality Project][Virtual Reality for the Internet] [ VOS: Next Generation Internet Communication][ http://interreality.org ] [ http://interreality.org/~tetron ][ pgpkey: pgpkeys.mit.edu 18C21DF7 ]
signature.asc
Description: Digital signature
_______________________________________________ vos-d mailing list vos-d@interreality.org http://www.interreality.org/cgi-bin/mailman/listinfo/vos-d