in reply to
Re: Why do my threads sometimes die silenty?
in thread Why do my threads sometimes die silenty?
I think what's happening is this:
- The master thread starts the slave thread
- The master thread then waits for messages from the slave to appear in a shared message queue
- The slave thread dies before it gets to send even a first message
- As a result, the master thread never joins the slave thread, and this causes the error message to not be printed
Below is a piece of code that illustrates this:
use strict;
use threads;
use threads::shared;
my $fct =
sub {
# eval {require Blah; Blah->import()} or print $@;
require Blah; Blah->import();
};
my $thr = threads->new($fct);
#$thr->join();
When executed as is, it produces the following output:
Perl exited with active threads:
1 running and unjoined
0 finished and unjoined
0 running and detached
If I remove the comment in front of the last line (the join call), then I get error message:
Thread 1 terminated abnormally: Can't locate Blah.pm in @INC (@INC con
+tains: etc...
Note also that the eval {} trick doesn't seem to make any difference. In other words, if I remove the comment in from of the eval {} line but leave the join() statement commented out, I still don't get the error message.
Note also that the silent dying does not happen if, for example, the error is caused by a division by zero instead of a non-existant include file.
Also interesting is the fact that if I do "use Blah;" instead of "require Blah; Blah->import()", then I see the error message, even if the join call is commented out.
In case you are wondering why I use a require instead of use, it's because loading the Blah module may end up loading modules which are not thread-safe. Therefore, I want to load the module at run-time, not at compile time (as explained on this node: http://www.perlmonks.org/?node_id=288022).