There's no need to lock the queue handle; and you need to broadcast on both branches otherwise you've a race condition that is responsible for your hangs; and the sleeps are not useful.

Try this:

use threads; use threads::shared; use Thread::Queue; my $a :shared = 0; my $q = Thread::Queue->new(); sub set_positive { while (1) { lock $a; if ($a == 0) { $a = 1+int(rand() * 100); ## Modified 10/03/2016 15:57 in +accordance with 1156654 & 1157305 $q->enqueue($a); print "At set_positive: $a\n"; } else { cond_wait($a) } cond_broadcast($a); } } sub set_zero { while (1) { lock $a; if ($a > 0) { $a = 0; $q->enqueue($a); print "At set_zero: $a\n"; } else { cond_wait($a) } cond_broadcast($a); } } sub printer { while (1) { my ($v_a, $v_b) = $q->dequeue(2); print "At printer $v_a $v_b ",$a, $/; } } my @threads = map threads->create($_), qw( set_positive set_zero print +er); $_->join for @threads;

Outputs:

C:\test>1156640 At set_positive: 11 At set_zero: 0 At set_positive: 98 At set_zero: 0 At set_positive: 2 At set_zero: 0 At printer 11 0 0 At printer 98 0 94 At printer 2 0 94 At set_positive: 94 At set_zero: 0 At set_positive: 61 At printer 94 0 0 At set_zero: 0 At printer 61 0 0 At set_positive: 13 At set_zero: 0 At printer 13 0 0 At set_positive: 13 At set_zero: 0 At printer 13 0 0 At set_positive: 28 At set_zero: 0 At printer 28 0 0 At set_positive: 18 At set_zero: 0 At printer 18 0 0 At set_positive: 59 At set_zero: 0 At printer 59 0 0 At set_positive: 15 At set_zero: 0 At printer 15 0 0 At set_positive: 31 At set_zero: 0 At set_positive: 22 At printer 31 0 0 At printer 22 0 0 At set_zero: 0 At set_positive: 19 At set_zero: 0 At printer 19 0 0 At set_positive: 2 At set_zero: 0 At printer 2 0 0 At set_positive: 22 At set_zero: 0

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Threads sharing global variable by BrowserUk
in thread Threads sharing global variable 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.