in reply to Calling exit() in thread does not call exit handler
calling exit() (I know, it is not recommended)
Then don't do that.
If you want to trap an abnormal exit from a thread, use eval & die just like you normally would:
use strict; use threads; use threads::shared; my $inthread : shared; # the exit handler (END block) is never called when a thread performs +an exit() # using a lower main sleep time will result in a normal exit of the ma +in thread. my $threadsleeptime : shared = 2; my $mainsleeptime = 5; my $thread = async { eval { $inthread = 1; sleep($threadsleeptime); print "Sleep in thread finished\n"; die('abnormal exit'); }; if( $@ =~ /^abnormal exit/ ) { ## do cleanup print "doing cleanup in thread\n"; } } sleep($mainsleeptime); print "Sleep in main finished\n"; $thread->join if ($thread); print "End of main program\n"; __END__ C:\test>junk29 Sleep in main finished Sleep in thread finished doing cleanup in thread End of main program
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Calling exit() in thread does not call exit handler
by kees (Initiate) on Oct 30, 2014 at 12:55 UTC | |
by BrowserUk (Patriarch) on Oct 30, 2014 at 13:16 UTC |