http://www.perlmonks.org?node_id=1047457


in reply to Re^5: killing threads inside forks
in thread killing threads inside forks

Actually you are right. I didn't real explained myself. I do want to terminate the threads childs and do some processing afterward (writing to a DB) , and then exit the child (fork) , and then exit the father. I got some answers here - and it really really helped, so thank you for that. I used the shared variable, and manaaged to kill all threads how I wanted. the problem now is that , if I dont kill the thread - I just get a hang , and nothing happens . I just don't know how to signal the sleep that all threads ended succesfully (and I want to join them). here is what I got so far :
$SIG{USR1} = sub {shutdown = 1;}; foreach my $functions_name (@func_arr){ #Start new thread my $t = threads->create (sub{ &$functions_name(@parameters); $SIG{KILL} = sub { print "\ngot to thread killer\n\n"; threads->exit(); }; print "\ngot to end of thread\n\n"; #$shutdown = 1; }); push(@threads,$t); } sleep until $shutdown; if (my @remain = threads->list(threads::running)) { foreach (@remain) { if ($_ != $$){ $_->kill('KILL')->detach; } } $shutdown = 0; testProcess::prc_kill_handler(); } print "All threads exited normally."; my $index=0; foreach my $thread (@threads) #wait for all threads untill the end +and insert results to hash { $hash_results{$index}=$thread->join; $index++; } return %hash_results;
Again ,this code actually works good when sending USR1 signal , but it doesn't when I'm not sending any signal , it just reaches to :
print "\ngot to end of thread\n\n";
and then nothing happens. how can I handle that ? thanks again guys ! sorry if I'm not so clear again...