Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

BEGIN {

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?

Thx

In reply to Can't start threads inside a BEGIN{} block by alain_desilets

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-29 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found