Hi, can someone explain what happens when you fork from a thread? In my case the script will get stuck.
Sample code that will not finish:
#!/usr/bin/perl
use threads;
sub something_1 {
my $thr;
foreach (1..10){
my $q = threads->create(\&something_2, $_);
$thr->{$_} = $q;
}
while (1) {
foreach (keys %$thr) {
if ($thr->{$_}->is_joinable()){
$thr->{$_}->join();
delete $thr->{$_};
}
}
print "trying:". (scalar keys %$thr)."\n";
last if not scalar keys %$thr;
sleep 1;
}
}
sub something_2 {
my $a = shift;
my $pid = fork();
if ($pid==0) {
print "cocot $a\n";
exit(0);
} else {
waitpid($pid, 0);
print "done $a\n";
}
}
# my $q = threads->create(\&something_1);
something_1;
Also I think I understand the following error, but can I get a clean exit somehow?
Perl exited with active threads:
9 running and unjoined
0 finished and unjoined
0 running and detached
EDIT
post a simpler example. the original one was with a fork from a thread from a thread
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.