Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Can't start threads inside a BEGIN{} block

by alain_desilets (Beadle)
on Sep 26, 2011 at 19:54 UTC ( #927936=perlquestion: print w/replies, xml ) Need Help??
alain_desilets has asked for the wisdom of the Perl Monks concerning the following question:

I'm starting this in a new node, even though it seems closely related to one I wrote last week: Why do my threads sometimes die silenty?.

I am trying to start a pool of threads inside a BEGIN{} block, as per the recommendation on Things you need to know before programming Perl ithreads. My reasons for doing this is that my program loads some modules which are not thread safe, so I want to make sure that the threads in the pool are started before those modules are actually instantiated.

Below is a piece of code which tries to do that.

my $num_threads = 2; my $thr = []; BEGIN { use threads; my $fct = sub { print "hello\n"; open FILE, ">>./hello.dat"; print FILE "hello\n"; close FILE }; for (my $ii=0; $ii<$num_threads; $ii++) { $thr->[$ii] = threads->create($fct); print "\$ii=$ii, started $thr->[$ii]$thr->[$ii]=$thr->[$ii]\n" +; $thr->[$ii]->join(); } } print "done\n";

However, when I run it as is, all I get is the final "done", and the file "hello.dat" doesn't even get created. However, if I comment out the line:


and its corresponding closing brace, I get the expected output

$ii=0, started threads=SCALAR(0x14d271c)threads=SCALAR(0x14d271c)=thre +ads=SCALAR(0x14d271c) hello $ii=1, started threads=SCALAR(0x14d273c)threads=SCALAR(0x14d273c)=thre +ads=SCALAR(0x14d273c) hello done

and the "hello.dat" file does get created and contains the two "hello" messages.

Can somebody explain to me why the threads don't actually run when they are inside a BEGIN {} block, but do when they aren't?


Replies are listed 'Best First'.
Re: Can't start threads inside a BEGIN{} block
by clueless newbie (Chaplain) on Sep 26, 2011 at 20:17 UTC
    Move the $num_threads/$thr in side the begin and it will work!
Re: Can't start threads inside a BEGIN{} block
by ikegami (Pope) on Sep 26, 2011 at 20:15 UTC

    Buggy version of Perl or threads?

    $ perl -E' BEGIN { use threads; async { say "foo"; } ->join for 1..3; } ' foo foo foo

      No, that's not it. Your problem has nothing to do with thread.

      Use «use strict; use warnings;»!!! $num_threads is undef when you execute the BEGIN block.

      1. The file is compiled.
        1. my $num_threads = 2; is compiled.
        2. my $thr = []; is compiled.
        3. BEGIN { ... } is compiled.
          1. ...
          2. for (my $ii=0; $ii<$num_threads; $ii++) { is compiled.
          3. ...
        4. BEGIN { ... } is executed.
          1. ...
          2. for (my $ii=0; $ii<$num_threads; $ii++) { is executed.
          3. ...
        5. print "done\n"; is compiled.
      2. The file is executed.
        1. my $num_threads = 2; is executed.
        2. my $thr = []; is executed.
        3. print "done\n"; is executed.
        Duh! Thx.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927936]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2018-01-19 02:48 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.