in reply to Re^4: is ||= threadsafe?
in thread is ||= threadsafe?

unless it is applied to a shared variable.

I've only talked about shared variables.

Any Perl operation applied to any shared variable is not thread-safe unless you take locks.

As I understood things, tye and you said Perl did the locking for the programmer when it came to the state of the SV. Now you're saying I need to take locks myself.

Replies are listed 'Best First'.
Re^6: is ||= threadsafe? (user locking)
by tye (Sage) on Oct 25, 2010 at 19:43 UTC

    For a shared variable, each thread has a separate instance of it, each tied to a single, hidden variable that holds the value. When reading the tied variable, GET magic locks the hidden variable, copies out its value, then unlocks it. When writing the tied variable, PUT magic locks the hidden variable, replaces its value, then unlocks it. You don't need to do your own locking for threads::shared variables in the simple case.

    You need to do locking if you have more than one (GET or PUT) operation and other iThreads might do other (GET or PUT) operations in between in a way that would cause problems. ||= does two operations so it is possible that extra locking needs to be done by the user (when dealing with shared variables). This likely means that even $x++ isn't iThreads safe on shared variables (in any case, I see no evidence of extra magic being implemented in the current threads::shared to make such safe -- probably easiest to do via overloading).

    - tye        

      Thanks, that's exactly what I understood the first time.
Re^6: is ||= threadsafe?
by BrowserUk (Patriarch) on Oct 25, 2010 at 18:45 UTC
    As I understood things, tye and you said Perl did the locking for the programmer when it came to the state of the SV. . Now you're saying I need to take locks myself.

    Now who being ... What was that word you called me?

    Perl takes care of protecting any internal changes.

    Ie. refcounts; flags; SV upgrades; UTF upgrades; anything that is not (normally) changed directly by the programmer.

    Eg, all 3 things you cited in your original post and many more.

    The programmer is responsible for locking the variable itself.


    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Now who being ... What was that word you called me?

      If I don't get it, then I can't possibly be playing dumb.

        If I don't get it, then I can't possibly be playing dumb.

        {Sigh} Ditto when the "one" you "wasted enough time with" doesn't exist. (Can we can the you said/I said stuff please?)


        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".
        In the absence of evidence, opinion is indistinguishable from prejudice.