in reply to baton passing threads and cond_signal
The "cond_signal" function takes a locked variable as a parameter and unblocks one thread that’s "cond_wait"ing on that variable. If more than one thread is blocked in a "cond_wait" on that variable, only one (and which one is indeterminate) will be unblocked.update: I overlooked your caveat...
The "cond_broadcast" function works similarly to "cond_signal". "cond_broadcast", though, will unblock all the threads that are blocked in a "cond_wait" on the locked variable, rather than only one.
The caveat I mentioned is that whilst you can reduce the sleep value to 0.0, if you comment out the sleeps entirely, even the cond_broadcast version hangs almost at once?
If I replace both cond_signal()s with cond_broadcast()s, it works fine, even with all the sleep()s removed completely.
You do still have a problem in that:
Could theoretically take a very long time. and$baton = int (rand ($no_of_threads)) until $baton != $id;
Never assigns the baton to thread number $no_of_sprinters (i.e. it won't work with only 1 sprinter)$baton = int (rand ($no_of_sprinters)) until $baton != 0;
Use:
Since rand($number) always returns a number LESS THAN $number$baton = int (rand ($no_of_sprinters)) + 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: baton passing threads and cond_signal
by Anonymous Monk on Aug 21, 2007 at 21:40 UTC | |
by Joost (Canon) on Aug 21, 2007 at 21:53 UTC | |
by Anonymous Monk on Aug 22, 2007 at 02:55 UTC | |
by Joost (Canon) on Aug 22, 2007 at 13:11 UTC | |
by Anonymous Monk on Aug 22, 2007 at 15:50 UTC | |
| |
by Joost (Canon) on Aug 22, 2007 at 17:02 UTC |