Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Creating threads inside BEGIN blocks

by sonofason (Sexton)
on May 16, 2006 at 08:14 UTC ( [id://549684]=perlquestion: print w/replies, xml ) Need Help??

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

Hello everyone,

In order to avoid copying too much data, it has previously been suggested (Things you need to know before programming Perl ithreads) that threads can be created inside a BEGIN block, before modules not used by the thread are loaded into memory. When you read the documentation for the threads module, however, it states in the list of bugs that:

"Creating threads inside BEGIN blocks (or during the compilation phase in general) does not work. (In Windows, trying to use fork() inside BEGIN blocks is an equally losing proposition, since it has been implemented in very much the same way as threads.)"

I tried running a simple example (not unlike the one in Things you need to know before programming Perl ithreads), in which I created a thread inside a begin block:

#!perl -w use strict; use threads (); my $test_thread; BEGIN { # start thread before modules are loaded in order to # prevent unnecessary copying of data to thread $|=1; $test_thread = threads->new(sub {for (1..10) { print "$_\n"; sleep + 1;}}); } use Math::BigInt; use IO::Handle; my $bi1 = Math::BigInt->new('1234567890'); my $bi2 = Math::BigInt->new('2345678901'); sleep 3; # give $test_thread some time to start up STDOUT->autoflush(1); print $bi2->bsub($bi1) . "\n"; $test_thread->join();

This produced the following output:

1
2
3
4
1111111011
5
6
7
8
9
10

As far as I can tell, the basic functionality seems to be working and running the script didn't generate any warnings on my system (Win XP using ActiveState Perl). Does anyone know what exactly it is that doesn't work with creating threads inside BEGIN blocks? Are there memory leaks after the thread has been terminated, or what is going on?

Thanks,
sonofason

Replies are listed 'Best First'.
Re: Creating threads inside BEGIN blocks
by liz (Monsignor) on May 16, 2006 at 08:31 UTC

    I think it depends very much on which version of Perl you are using, and lately, which version of threads.pm you are using (as threads now is dual-life, so more up to date on CPAN than any non-blead/maint perl).

    My personal experience on Linux was that some warnings would occur on process exit. I haven't seen those for a while now. But then again, I haven't been doing much with threads the past 2.5 years anyway.

    I hope someone with more up-to-date information will be more able to enlighten you.

      Thanks, I am using version 5.8.8 build 587 for Win32 and whichever version of threads.pm that is bundled with that (looks like ver 1.12), but I should probably update to the latest version...

Re: Creating threads inside BEGIN blocks
by BrowserUk (Patriarch) on May 16, 2006 at 09:56 UTC

    I've been doing this since 5.8.3 and have never encountered any particular limitations. That is not to say that there are none, just that I haven't encountered them.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Creating threads inside BEGIN blocks
by jdhedden (Deacon) on May 16, 2006 at 14:14 UTC
    On a minor note, in some implementations of Perl, returning a closure from a thread may cause the interpreter to crash. At a minimum, it will result in an (apparently harmless) warning of leaked scalars when the interpreter exits.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found