in reply to Re: is a readonly hash thread safe ?
in thread is a readonly hash thread safe ?

but i pass the reference to the hash as a parameter for thread create, so im using the only hash defined in the main block ?

Replies are listed 'Best First'.
Re^3: is a readonly hash thread safe ?
by Corion (Patriarch) on Jul 15, 2009 at 09:24 UTC

    Even that reference is just a copy. See perlthrtut and/or threads::shared. Perl ithreads are based off the idea of the Win32 fork emulation, which is why every thread gets its own copy of (almost) everything. This likely is the only possible implementation anyway without risking lots of locks like Python has with its much maligned Global Interpreter Lock, as even reading a Perl variable means writing to shared memory (the refcount or integer slots for example) otherwise.

      Perl has also serious problems with LOCKs, using a simple REGEX in several thread instances of the same program causes LOCKs to go HIGH. And this is valid not only for REGEX's. See node
        tell it to perl5-porters, please :)
      i pass a hash-reference to the thread, so the thread gets (a copy of) the reference - BUT the data referenced by the hash-reference should be only once in memory ?

        No. The reference is a copy of the reference, and the data is a copy of the referenced data. Again, see perlthrtut, and threads::shared. Threads don't share things as you might come to expect if you think of C-level threads.

Re^3: is a readonly hash thread safe ?
by Anonymous Monk on Jul 15, 2009 at 09:27 UTC
    No, each thread gets its own copy. Read the tutorial, it explains the basics.