Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to Optionally enable threads in Perl 5.10.0

by DrWhy (Chaplain)
on Aug 09, 2010 at 17:48 UTC ( [id://853887]=perlquestion: print w/replies, xml ) Need Help??

DrWhy has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE: Based on the responses below and my own experimentation, this is starting to look like a doc bug...

ORIGINAL:

Hi Monks,

I need to be able to only enable threads in an application if it run under perl 5.10.0 or higher. Reading the threads docs for perl 5.10.0, you see this:

It is also important to note that you must enable threads by doing use threads as early as possible in the script itself, and that it is not possible to enable threading inside an eval "" , do, require, or use.

That pretty much takes care of all the ways I had thought of to accomplish this. So does anyone here know of way around this so that when run under perl 5.8.8 and below it doesn't try to use threads and with higher versions it is made available?

Thanks,

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

  • Comment on How to Optionally enable threads in Perl 5.10.0

Replies are listed 'Best First'.
Re: How to Optionally enable threads in Perl 5.10.0
by ikegami (Patriarch) on Aug 09, 2010 at 18:13 UTC
    I don't know what they're talking about.
    $ /usr/bin/perl -v This is perl, v5.10.0 built for i486-linux-gnu-thread-multi ... $ perl -e'eval "use threads; async { print qq{ok\n} }->join;"' ok

    Maybe it's referring to symbol import? That passage is not present in newer version of Perl.

    Mind you, it doesn't really make much sense to load threads conditionally. It seems to me it would make more sense to choose which module to use conditionally, one that uses uses threads, one that doesn't.

    my $engine; if ($use_threads) { require Engine::Threaded; $engine = Engine::Threaded->new(); } else { require Engine::Fallback; $engine = Engine::Fallback->new(); }
      Yeah, I did the same thing just now:
      C:\Documents and Settings\Mine>c:\Perl5.10\bin\perl.exe -e "eval 'use +threads'; print \"$@\"; $t1 = threads->create(sub {print '1'}); $t2 = + threads->create(sub{print '2'}); $t1->join; $t2->join();" 12 C:\Documents and Settings\Mine>
      Requiring in diff't modules in the case at hand doesn't make much since. I'm only doing thread work in the main script, so the '*threaded' module would just contain the 'use threads' statement and the '*notthreaded' module would be empty (or just wouldn't exist).

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Re: How to Optionally enable threads in Perl 5.10.0
by BrowserUk (Patriarch) on Aug 09, 2010 at 18:36 UTC

    I've been evaling threads into existence in my REPL for as long as can remember.

    Each line (or set of lines) below is evaled when a double colon ;; is seen:

    [0] Perl> use threads; use threads::shared;; [Bad file descriptor] Perl> async{ sleep 5; print, sleep 1 for 1.. 10 }->detach; print, sleep 1 for 1.. 20;; 1 2 3 4 5 1 6 2 7 3 8 9 4 10 5 6 11 7 12 8 13 9 14 10 15 16 17 18 19 20

    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: How to Optionally enable threads in Perl 5.10.0
by Anonymous Monk on Aug 09, 2010 at 17:53 UTC
    Sure
    use MyJunk;
    package MyJunk; if( $] > 5.008008 ){ require MyJunk::threads; } else { require MyJunk::nothreads; }
      That's interesting, but if it's unsafe to 'require threads' directly, seems that this approach is just doing the same thing indirectly and would suffer the same kind of problem. If the problem is just that you must be sure that threads is there at compile time maybe something like this would work:
      BEGIN { if ($] > 5.008008) { require threads; } }

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2025-03-16 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (54 votes). Check out past polls.