combinations of boost::condition
Is there any way to wait for one of several condition objects to become ready? Is there some reason that this capability is omitted from the thread library? I would like to do something like this: mutex monitorA, monitorB; condition conditionA, conditionB; double_condition conditionAB(conditionA,conditionB); void executeNextEvent() { lock lkA(monitorA); lock lkB(monitorB); //Wait for either conditionA or conditionB to become ready. conditionAB.wait(lkA,predicateA,lkB,predicateB); if(predicateA()) { executeEventA(); } else { executeEventB(); } } [Non-text portions of this message have been removed]
Joshua Napoli wrote:
Is there any way to wait for one of several condition objects to become ready? Is there some reason that this capability is omitted from the thread library?
;-) ... "What you are asking for is like wanting to sleep in two or more beds at the same time. This, of course, is only possible if you are the president of the United States." http://google.com/groups?selm=slrn7hiau9.1q3.kaz%40ashi.FootPrints.net (Subject: Re: pthreads and waiting for MULTIPLE conditions) Note that for "cominations" of predicates [which is what you probably need], you'll have to use broadcast() [and never use signal()] unless all of your possible waiters can "handle" each and every predicate that you "combine". You should also protect all these predicates with *one single mutex* because the use of different mutexes for concurrent waiting on the same condition variable isn't allowed: http://tinyurl.com/bd0v ---- Dave Butenhof wrote: [...]
When a thread waits on a condition variable, having specified a particular mutex to either the pthread_cond_timedwait() or pthread_cond_wait() operation, a dynamic binding is formed between that mutex and condition variable that remains in effect as long as at least one thread is blocked on the condition variable. During this time, the effect of an attempt by any thread to wait on that condition variable using a different mutex is undefined. Once all waiting threads have been unblocked (as by the pthread_cond_broadcast() operation), the next wait operation on that condition variable shall form a new dynamic binding with the mutex specified by that wait operation. Even though the dynamic binding between condition variable and mutex may be removed or replaced between the time a thread is unblocked from a wait on the condition variable and the time that it returns to the caller or begins cancellation cleanup, the unblocked thread shall always re-acquire the mutex specified in the condition wait operation call from which it is returning.
regards, alexander.
participants (2)
-
Alexander Terekhov
-
Joshua Napoli