To ignore the thread that is running, return control and throw away everything that it can output, the correct method to use is detach , not exit .
See perldoc perlthrtut - Ignoring a stream .
perldoc threads explains why the code exits:
threads->exit()
If necessary, a thread can be displayed at any time by calling threads->exit() . This will cause the stream to return undef in a scalar context or an empty list in the list context. When called from the main thread, it behaves the same as exit(0) .
Perhaps there is a way to achieve instant completion (does not work for me on Windows):
use threads 'exit' => 'threads_only';
This globally overrides the default behavior of the exit() call inside the thread and effectively makes such calls behave the same as threads->exit() . In other words, with this setting, calling exit() only causes the thread to terminate. Due to its global effect, this setting should not be used inside modules or the like. This parameter does not affect the main thread.
The documentation also offers another way using the set_thread_exit_only method (again, does not work for me on Windows):
$thr->set_thread_exit_only(boolean)
This can be used to change the behavior of the output stream only for the stream after it is created. With a true argument, exit() will only result in a stream to exit. With a false argument exit() will terminate the application. This call does not affect the main thread.
The following example uses the kill signal to terminate the $unwanted stream:
use strict; use warnings; use threads; my $unwanted = threads->create( sub { local $SIG{KILL} = sub { threads->exit }; sleep 5; print "Don't print me!\n"; } ); my $valid = threads->create( sub { sleep 2; print "This will print!\n"; } ); $unwanted->kill('KILL')->detach;