views:

56

answers:

3
+1  Q: 

Two mutex condvar

If I have 2 mutexes locked, and I have a condvar for each one, is there an easy way to wait for either condvar to fire? I want to leave holding both locks again, and with (at least) one of the condvars having been signaled.

A: 

no easy way that i can see, i would just create a third condvar and mutex, because you are really waiting for a different condition.

luke
A: 

No. You end up with a race condition. Imagine both signals are sent before pthread_cond_wait returns. One gets thrown away, and they're indistinguishable.

Another way to think, as Luke says, is that there are two different conditions happening. When you get the signal, you double-check the condition. If there are two conditions, there is clearly a race condition if the other condition may change while you are checking it.

The solution is to have two different threads. If those threads would need to be exclusive, use another lock to synchronize them.

Potatoswatter
A: 

Redesign? Without knowing much about what you want to do: Try using one mutex and multiple condvars. If you want to wait for either condvar make a third condvar that is fired whenever the first one or second one is fired (or something like this).

sellibitze
Unfortunately this is for a test where I can't really get at one of the mutexes. I guess I'll just keep mocking out more stuff until I'm down to one mutex.
Adam Berkan