I recently added
thead signalling capabilities to the
threads module, and improved the functionality of calling
exit inside a thread. These features can be used to provide
kill/cancel threads as follows:
use threads;
sub thr_func
{
# Thread 'cancellation' signal handler
$SIG{'KILL'} = sub { threads->exit(); };
...
}
# Create a thread
my $thr = threads->create('thr_func');
...
# Signal the thread to terminate, and then detach
# it so that it will get cleaned up automatically
$thr->kill('KILL')->detach();
Be sure to get the latest version of the
threads module from CPAN, and read the POD for more information and caveats.
Remember: There's always one more bug.