I am writing a perl script to exit a thread after 5sec as shown below. but I am not able to print the message from if condition.
Output of the script is Alarm clock
How to print proper error messages from the parent process when timeout for threads.
#!/usr/bin/perl
use strict;
use warnings;
use threads;
my $th = threads->create( \&sub1 );
my $thid = $th->tid();
eval
{
$SIG{'KILL'} = sub
{
if ( $th->is_running() )
{
print "timed out, exiting the thread $thid\n";
$th->exit();
}
};
alarm(5);
$th->join();
};
sub sub1
{
my $i = 0;
while ( $i == 0 )
{
}
}