in reply to Re: Semaphores failing to prevent race condition
in thread Semaphores failing to prevent race condition

Thread safety isn't that important, if you stick to the rule of thumb to only require the unsafe modules you need in child threads.

You're confusing threads and processes here. Perl code is shared across all of the process's threads. It doesn't matter when you require the module.

My understanding is, there is only thread-safe or not thread-safe

I usually classify as:

  • Comment on Re^2: Semaphores failing to prevent race condition

Replies are listed 'Best First'.
Re^3: Semaphores failing to prevent race condition
by Anonymous Monk on Jun 01, 2011 at 18:18 UTC
    You're confusing threads and processes here.

    No I am not. If you use Tk; or some other module, and then you use threads, instead of using require Tk;, you'll run into segfaults, etc etc. Using require Tk; avoids the issue completely.

      Then your rule of thumb is wrong. "Only require the unsafe modules you need in child threads" doesn't make sense. Requiring a module in one thread loads it for the entire process. Perhaps you misstated what you meant?

      Or not:

      $ cat Mod.pm package Mod; print __PACKAGE__, "\n"; 1; $ perl -Mthreads -e'my @t = map { async { require Mod; }; } 1..2; $_-> +join() for @t;' Mod Mod
        Requiring a module in one thread loads it for the entire process.

        The script below traces out the content of %INC (as proof of what is loaded) three times.

        1. Once in main before a thread requires Tk.

          No Tk is visible to the main thread.

        2. Once in the thread after it has required Tk.

          The Tk modules are now visible. Within the thread.

        3. Once again in main after the thread has required Tk.

          The Tk modules remain invisible to the main thread.

        Ergo. Anonymonk is right. You are wrong.

        Live with it but don't talk about it cos no one is interested in how you are going to try to justify not just a wrong statement, but one where you tell someone who is so obviously correct, that they are wrong.

        The oh so complicated test code:

        #! perl -slw use strict; use threads; async{ sleep 4; require Tk; print for '2 ', keys %INC; }->detach; sleep 2; print for '1 ', keys %INC; sleep 6; print for '3 ', keys %INC;

        And the proof:

        C:\test>junk 1 XSLoader.pm warnings/register.pm strict.pm C:/Perl64/site/lib/sitecustomize.pl warnings.pm threads.pm overload.pm Config.pm 2 re.pm C:/Perl64/site/lib/sitecustomize.pl ActiveState/Path.pm Cwd.pm warnings.pm Tk/Wm.pm Symbol.pm C:/Perl64/site/lib/auto/Tk/getEncoding.al Exporter.pm Encode/Unicode.pm C:/Perl64/site/lib/auto/Tk/autosplit.ix Tk/Toplevel.pm File/Spec.pm File/Spec/Win32.pm Encode/Byte.pm warnings/register.pm XSLoader.pm Encode/Alias.pm Tk/Frame.pm Encode/Config.pm C:/Perl64/site/lib/auto/Tk/Toplevel/autosplit.ix Encode/Encoding.pm Tk/Pretty.pm Tk/Widget.pm Config_git.pl threads.pm base.pm Encode.pm C:/Perl64/site/lib/auto/Tk/Wm/autosplit.ix Tk/MainWindow.pm Tk/Submethods.pm Config.pm File/Basename.pm ActivePerl/Config.pm C:/Perl64/site/lib/auto/Tk/Widget/autosplit.ix Tk/Image.pm Carp.pm Tk/Event.pm bytes.pm File/Spec/Unix.pm Tk/Event/IO.pm vars.pm strict.pm Tk/Derived.pm constant.pm C:/Perl64/site/lib/auto/Tk/Frame/autosplit.ix Config_heavy.pl Tk/Configure.pm Tk/After.pm overload.pm AutoLoader.pm Tk/CmdLine.pm DynaLoader.pm Tk.pm 3 XSLoader.pm warnings/register.pm strict.pm C:/Perl64/site/lib/sitecustomize.pl warnings.pm threads.pm overload.pm Config.pm

        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.