in reply to Re^6: baton passing threads and cond_signal
in thread baton passing threads and cond_signal
If first thread signals, but gets interrupted before it can loop back to re-lock the variable and reenter the wait
If that's the problem (and I can't see what else it could be), then all you have to do is change
towhile (1) { lock ($baton); cond_wait ($baton) until $baton == $id; ... cond_broadcast ($baton); }
lock ($baton); while (1) { cond_wait ($baton) until $baton == $id; ... cond_broadcast ($baton); }
While you should do that change, I don't think that's the problem. The only time where missing the signal would cause a problem is if it's sent between the time $baton == $id is checked and the time cond_wait blocks. The purpose of locking $baton is to create the mutual exclusion that should prevent this from happening.
The problem might be that your system's implementation of cond_wait isn't atomic (while it should be), allowing a signal to come in after cond_wait unlocks $baton, but before cond_wait starts waiting.
You could give yourself a safety net by using cond_timedwait — cond_wait with a timeout — to check $baton periodically.
Update: Did some repharsing and added the second last paragraph.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: baton passing threads and cond_signal
by Anonymous Monk on Aug 22, 2007 at 16:25 UTC | |
by ikegami (Patriarch) on Aug 22, 2007 at 16:34 UTC | |
by Anonymous Monk on Aug 22, 2007 at 16:47 UTC | |
by BrowserUk (Patriarch) on Aug 22, 2007 at 19:00 UTC | |
by ikegami (Patriarch) on Aug 22, 2007 at 19:32 UTC | |
|