in reply to Re^2: point #4 (Now what was that you were saying?)
in thread Thoughts on how to devise a queryable win32 service

Ok, I found it. It's in the perlthrtut man page that comes with Perl 5.8.5. At the end of the section titled "Shared And Unshared Data", it says:

Note that a shared variable guarantees that if two or more threads try to modify it at the same time, the internal state of the variable will not become corrupted. However, there are no guarantees beyond this, as explained in the next section.

And the next section describes some typical race condititons one might encounter (two threads trying to read, then increment the same variable).
So anyway, that text has led me to believe that a shared array used to implement a queue for passing data between threads would be safe, even with no locking, since the push() and shift() operations would be atomic in nature.
If this doesn't apply to SMP machines though, then I've got some code to fix...

Replies are listed 'Best First'.
Re^4: point #4 (Now what was that you were saying?)
by BrowserUk (Patriarch) on Feb 10, 2005 at 17:47 UTC

    Thankyou for taking the time to find it again.

    It's also nice to have my observations confirmed, and to see that it is mentioned in the documentation somewhere.

    It would be nice it also got a mention in the threads::shared POD, along with a few examples of when lock needs to be used and when not. I'll try to produce a docu-patch for that.

    So anyway, that text has led me to believe that a shared array used to implement a queue for passing data between threads would be safe, even with no locking, since the push() and shift() operations would be atomic in nature.

    I've generally use Thread::Queue for implementing queues between threads. I tried it early on and it has never given me a problem, so I've stuck with it.

    Re-reading perlthrtut, I agree with your reading that push and shift should be safe for this purpose. I'll probably have a play and see if I can break them.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.