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

I once enjoyed browsing through the articles on PerlMonks. What isn't fun is the rudeness towards other monks. Why the rudeness? But maybe, ikegami was rude in his response to your demonstration.

The interesting thing is that BrowserUk's demonstration runs with the lock obtained outside the while loop. This is also true on the Windows platform.

sub set_positive { lock $a; while (1) { ... } } sub set_zero { lock $a; while (1) { ... } }

A long time monk ikegami made a correct statement when stating the lock is re-obtained before cond_wait returns. In fact, that is mentioned in the threads::shared documentation.

Another finding is the printer output containing "0 0" indicating set_zero enqueued twice in a row. Also, the output contains two positives indicating set_positive enqueued twice in a row. These occur often in the output. The OP does not mention if such occurrence is valid, so am not sure.

Is there a code of conduct for PerlMonks? The reason is that there is no joy in seeing monks attacking one another. Recently, the Mojolicious team added a section titled Code Of Conduct to a guide. Is there anything like that for PerlMonks?

Replies are listed 'Best First'.
Re^11: Threads sharing global variable (disingenous)
by Anonymous Monk on Mar 10, 2016 at 06:25 UTC

    he interesting thing is that BrowserUk's demonstration runs with the lock obtained outside the while loop.

    Except that it doesn't, thats ikegamis code -- get some sleep or something

      It does and shown here. Seeing two zeros in the output happens either way with the lock obtained inside or outside the while loop. The OP doesn't mention if such occurrence is valid. Therefore, likely not a problem.

Re^11: Threads sharing global variable (disingenous)
by BrowserUk (Patriarch) on Mar 10, 2016 at 09:26 UTC
    Why the rudeness?

    History, correctness, and bare-faced lies.

    And because sometimes life is too short to be polite about crap code.

    And because you don't become an expert in anything by reading the manual, except for "expert at reading the manual".

    Real expertise requires you to have written real code, that's been run in real, live systems, and to have debugged it, and found the limitations of the manual's api descriptions.


    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^11: Threads sharing global variable (disingenous)
by Anonymous Monk on Mar 10, 2016 at 05:19 UTC

    Today, am not getting 2 positives on the same line. But am still getting two zeros when running the solution posted by BrowserUk.

    ... At printer 0 0 ... At printer 0 0 ... At printer 0 0 ... At printer 0 0 ...

    The OP does not mention if this is valid. Therefore, maybe okay.

      If you were running my posted code, you'd be getting 3 numbers on each "At printer ... " line.


      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.
        1156646.pl | egrep "printer 0 0" ... At printer 0 0 59 At printer 0 0 0 At printer 0 0 0 At printer 0 0 0 At printer 0 0 0 At printer 0 0 54 At printer 0 0 0 At printer 0 0 0 At printer 0 0 0 At printer 0 0 0 At printer 0 0 88 At printer 0 0 0 At printer 0 0 74 ...
        use threads; use threads::shared; use Thread::Queue; my $a :shared = 0; my $q = Thread::Queue->new(); sub set_positive { lock $a; while (1) { if ($a == 0) { $a = int(rand() * 100); $q->enqueue($a); # print "At set_positive: $a\n"; } else { cond_wait($a) } cond_broadcast($a); } } sub set_zero { lock $a; while (1) { if ($a > 0) { $a = 0; $q->enqueue($a); # print "At set_zero: $a\n"; } else { cond_wait($a) } cond_broadcast($a); } } sub printer { my ( $v_a, $v_b ); my $cnt = 100000; while ( 1 ) { ( $v_a, $v_b ) = $q->dequeue( 2 ); print "At printer $v_a $v_b ",$a, $/; # print "At printer $v_a $v_b\n"; exit unless --$cnt; } } threads->create( $_ ) for qw( set_positive set_zero printer ); $_->join() for threads->list;