At first glance it appears that you should indeed use cond_broadcast() since you're cond_wait()ing on the baton from multiple threads, but you only want one, specific thread to pick it up. From the docs (emphasis mine):
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.

...

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.

update: I overlooked your caveat
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:

$baton = int (rand ($no_of_threads)) until $baton != $id;
Could theoretically take a very long time. and

$baton = int (rand ($no_of_sprinters)) until $baton != 0;
Never assigns the baton to thread number $no_of_sprinters (i.e. it won't work with only 1 sprinter)

Use:

$baton = int (rand ($no_of_sprinters)) + 1;
Since rand($number) always returns a number LESS THAN $number


In reply to Re: baton passing threads and cond_signal by Joost
in thread baton passing threads and cond_signal by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.