in reply to Re^16: Threads sharing global variable (disingenous)
in thread Threads sharing global variable

The following resolves the printer output having two zeros. That led to the confusion. It looks like somebody already mentioned it.

$a = int(rand() * 100) + 1;

The OP presented an interesting problem. Seeing the double zeros led me on a wrong path. Please, for future monks testing your solution, can you kindly add + 1 inside set_positive.

sub set_positive { lock $a; while (1) { if ($a == 0) { $a = int(rand() * 100) + 1; $q->enqueue($a); print "At set_positive: $a\n"; } else { cond_wait($a) } cond_broadcast($a); } }

Thank you.

Replies are listed 'Best First'.
Re^18: Threads sharing global variable (disingenous)
by BrowserUk (Patriarch) on Mar 10, 2016 at 16:02 UTC
    can you kindly add + 1 inside set_positive.

    I've been reluctant to make *any* modifications to my posted code lest it become a source of "you changed your code" contention, and a rod for my back.

    Hopefully, that possibility has now past and the change is made.


    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.
Re^18: Threads sharing global variable (disingenous)
by Anonymous Monk on Mar 10, 2016 at 15:57 UTC

    I forgot to place the lock back inside the while loop. Sorry about that. The + 1 is necessary to not have double zeros in the output.

    sub set_positive { while (1) { lock $a; if ($a == 0) { $a = int(rand() * 100) + 1; $q->enqueue($a); print "At set_positive: $a\n"; } else { cond_wait($a) } cond_broadcast($a); } }