use strict; use threads; use threads::shared; my $inthread : shared; END { print "Exit handler called (inthread=$inthread)\n"; } # 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 main thread. my $threadsleeptime : shared = 5; my $mainsleeptime = 2; my $thread = async { $inthread = 1; sleep($threadsleeptime); print "Sleep in thread finished\n"; # force also main thread to exit # but bypasses exit handler in END block exit(1); } sleep($mainsleeptime); print "Sleep in main finished\n"; $thread->join if ($thread); print "End of main program\n";