Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: Threads sharing global variable

by Tanktalus (Canon)
on Mar 05, 2016 at 00:14 UTC ( #1156863=note: print w/replies, xml ) Need Help??


in reply to Re^3: Threads sharing global variable
in thread Threads sharing global variable

I grabbed ikegami's code and ran it against perls 5.8.8, 5.10.1, 5.16.1, 5.18.1, and 5.22.1 on Linux/amd64, and it spat out huge amounts of data for each one. So, if you're only getting a single line of output, I have to wonder what's so different in your environment as to break this. However, that it works here indicates that the locking isn't a problem by itself.

Replies are listed 'Best First'.
Re^5: Threads sharing global variable
by BrowserUk (Pope) on Mar 05, 2016 at 02:00 UTC
    I have to wonder what's so different in your environment as to break this

    Simple.

    threads::shared cond_* variables and associated APIs, map directly to Pthreads condition variables; thus, they have a fair chance of working on platforms that have native pthreads.

    Conversely, Windows has no direct analogy to pthreads condition variables, hence they have to be emulated using those synchronisation primitives that Windows does provide. That emulation is done crudely, and badly and has to my knowledge never worked correctly. Hence why I have avoided them like the plague.

    For the most part, if used sensibly -- which means not relying on the (long identified as broken) edge-trigger semantics that underly cond_wait() -- threads::shared's emulated cond_vars can be used on Windows; but doing so successfully requires an understanding of their inherent limitations (which exist everywhere, but are exacerbated by the crude emulation on Windows), and that requires that the designer has actually written and debugged real-life code doing real work; not just thrown together a few do-nothing, trivial examples for the purposes of establishing cred.

    The sad part is, Window's native synchronisation primatives are extremely rich and powerful; and do not suffer the Achilles heel of Pthread's one and only synchronisation mechanism -- namely, spurious triggers.

    For Far more than You Ever Want To Know about the problems of trying to accurately emulate Pthreads cond_vars (broken) semantics on windows see: 3. Implementing Condition Variables on Win32.


    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^5: Threads sharing global variable
by beech (Parson) on Mar 05, 2016 at 00:20 UTC
    Hi Tanktalus, I'm running on windows

    update:

    FWIW, if I only move the lock in set_positive inside the while loop, still just one line of output.

    If I only move the lock in set_zero inside the while loop, a couple thousand lines of output, and then it locks hangs

    If I move both the lock statements inside the while loop, like BrowserUk wrote, it just keeps running and doesn't hang

      For me, it works fine in Windows too.
      C:\Users\ikegami>perl a.pl At printer 2 0 At printer 93 0 At printer 94 0 At printer 60 0 At printer 26 0 At printer 83 0 At printer 65 0 At printer 45 0 At printer 8 0 At printer 20 0 At printer 7 0 At printer 47 0 At printer 53 0 At printer 22 0 At printer 26 0 At printer 22 0 At printer 24 0 At printer 51 0 At printer 13 0 At printer 100 0 ...
      This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x +64-multi-thread

        This is your most disingenuous post to date. (The latest in a very long line.)

        Below is a simple experiment using your exact posted code from Re^2: Threads sharing global variable; that anyone running windows and a few versions of perl installed, can conduct for themselves in around five minutes, and see your dishonesty for themselves.

        This is your code running under This is perl, v5.10.1 built for MSWin32-x64-multi-thread:

        Summary: It produces 287(*) lines of output before hanging. (Varies from run to run; but that's typical.)

        This is your code running under This is perl 5, version 18, subversion 4 (v5.18.4) built for MSWin32-x64-multi-thread:

        Summary: It produces 817* lines of output. (Usually runs a little longer than under 5.10, but of 20 runs this was the most output before hanging.)

        This is your code running under This is perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x64-multi-thread:

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1156863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2021-09-26 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?