Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: is ||= threadsafe?

by BrowserUk (Pope)
on Oct 25, 2010 at 17:29 UTC ( #867298=note: print w/replies, xml ) Need Help??


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

||= happens in Perl code, yet it's not thread safe.

You're still not getting it.

  • ||= is thread-safe, just as with any other operation--unless it is applied to a shared variable.

    That's the basic tenet of threads. Everything(*) is thread-safe unless it involves explicitly shared variables. Clear and unambiguous.

    It is the very raison d'etre of ithreads.

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

    That the basic tenet threads::shared. Again, clear and unambiguous.

There is no purpose in documenting the thread-safety of every opcode--or internal operations--because they are all completely covered by those two simple tenets

And that is what sets iThreads apart, and above, any other shared-state concurrency implementation. You use it exactly as if it was non-threaded, with no shared-state, even for the nasties of interpreted language threading--fat variables & closures. Except where you need shared-state, then you mark it explicitly and take the appropriate precautions.

Sure, the implementation could be more efficient--of time and memory. And the user could be given finer-grained control. But for the most part, inventing a model that allows so many pre-threading modules to just work inside threads is a remarkable achievement. Even the majority of non-Pure Perl modules function perfectly within iThreads, and that's truly remarkable, and no accident.

Artur Bergman should be lauded.

(*)For the pedants: External state excluded.


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.

Replies are listed 'Best First'.
Re^5: is ||= threadsafe?
by ikegami (Pope) on Oct 25, 2010 at 17:46 UTC

    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.

      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.
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://867298]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-12-16 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (454 votes). Check out past polls.

    Notices?