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;
In reply to Re: baton passing threads and cond_signal
by Joost
in thread baton passing threads and cond_signal
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |