Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

is ||= threadsafe?

by perl-diddler (Hermit)
on Oct 24, 2010 at 23:33 UTC ( #867104=perlquestion: print w/ replies, xml ) Need Help??
perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:

I'm using a dynamically allocated queue, that I init with ||=, ala:
$PageQueue ||= Queue->new;
Since this is a 'test & set', and a typical location for a race condition, am I correct in assuming that this needs to be locked? during the test&set, like:
{ lock $PageQueue; $PageQueue ||= Queue->new; }
Thanks!

Comment on is ||= threadsafe?
Select or Download Code
Re: is ||= threadsafe?
by BrowserUk (Pope) on Oct 25, 2010 at 02:17 UTC
    Since this is a 'test & set',

    It certainly isn't implemented using an atomic instruction, so yes. If $PageQueue is a shared variable, and the statement is part of a thread function body that may be instantiated more than once, you need to lock it before setting it.


    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.
Re: is ||= threadsafe?
by ikegami (Pope) on Oct 25, 2010 at 03:44 UTC

      Without contesting any of those in any way whatsoever, can you suggest possible scenarios in which they might cause some kind of problem?

        segmentation fault, premature deallocation and segmentation fault (respectively) are serious problems in any scenario.

      Admittedly, I have only waited roughly an hour for this to run, but that is sufficient for 100 threads to explore the possibilities of each your speculative modes of failure, for 100 million plus iterations each. (Ignoring all the other threaded code I've ever written that could have done so)

      I consider that a pretty conclusive demonstration that you're talking out your a the top of your head.

      Of course you could have tried this trivial test of your speculations yourself, in the same minimal amount of time, before posting such FUD, but that wouldn't meet your apparent motivations.


      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.

        Yeesh, what's with all the hostility?

        See Tye's buried node for a sensible answer. Unfortunate that it wasn't posted at a more visible depth.

        Stop pretending I said it was unsafe.

        The idea is that someone would indicate if it was or not. It would have taken 30s of my time and 30s of theirs.

      But the increment doesn't happen in user code. It happens internally in the perl code, which is guaranteed to be thread safe (unless there is a bug). So this wouldn't be an issue. the issue I queried about is not, clearly, something that is internal, but is a short hand for a user-land test and then set, thus while it *could* be optimized, I sorta doubted it, but thought I'd check.

      The case you are talking about would imply a bug in perl in it not being thread safe, since variable reference counts are not user-land features, but things handled automatically by perl -- so they are inherently thread-safe as much as perl is defined to be thread-safe.

      That said, I'm sure you'd have to protect such increments from multi-threaded access, inside perl, but that's not something we need to worry about in userland.

        It happens internally in the perl code, which is guaranteed to be thread safe

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

        You may say that's because it's the user requesting a change the variable, but that's the case for all the examples I showed too.

        Now, tye and BrowserUk say the shared SV is appropriately locked internally when updated (and I believe it to be true), so that eliminates the potential problems I posited, so the point is simply that your guarantee is worthless as formulated.

        That's probably because you pulled the guarantee out of this air. When I looked this up some time ago, I couldn't find any documentation on this. A very quick look still shows nothing. This is why I prompted for replies on the subject.

        Update: Added everything after first line.
        Update: Added "for shared variables" in response to reply.

Re: is ||= threadsafe?
by ambrus (Abbot) on Oct 25, 2010 at 10:33 UTC

    What's more, it can't be implemented as thread safe, because the ||= operator must evaluate its rhs only if the lhs is false, and the expression on the rhs can have any side effect, including waiting a lot, so it would be bad to lock all other threads out during that time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://867104]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-10-01 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (38 votes), past polls