title: timeout for threads in perl
body: <p>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.</p>
  
  <p>Output of the script is Alarm clock</p>
  
  <p>How to print proper error messages from the parent process when timeout for threads.</p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  use warnings;
  use threads;
  
  my $th   = threads-&gt;create( \&amp;sub1 );
  my $thid = $th-&gt;tid();
  
  eval
  {
      $SIG{'KILL'} = sub
      {
          if ( $th-&gt;is_running() )
          {
              print "timed out, exiting the thread $thid\n";
              $th-&gt;exit();
          }
      };
      alarm(5);
      $th-&gt;join();
  };
  
  sub sub1
  {
      my $i = 0;
      while ( $i == 0 )
      {
      }
  
  }
  </code></pre>