sonofason has asked for the wisdom of the Perl Monks concerning the following question:
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 | |
by sonofason (Sexton) on May 16, 2006 at 08:51 UTC | |
|
Re: Creating threads inside BEGIN blocks
by BrowserUk (Patriarch) on May 16, 2006 at 09:56 UTC | |
|
Re: Creating threads inside BEGIN blocks
by jdhedden (Deacon) on May 16, 2006 at 14:14 UTC |