Friday, 15 May 2015

multithreading - timeout for threads in perl -



multithreading - timeout for threads in perl -

i writing perl script exit thread after 5sec shown below. not able print message if condition.

output of script alarm clock

how print proper error messages parent process when timeout threads.

#!/usr/bin/perl utilize strict; utilize warnings; utilize threads; $th = threads->create( \&sub1 ); $thid = $th->tid(); eval { $sig{'kill'} = sub { if ( $th->is_running() ) { print "timed out, exiting thread $thid\n"; $th->exit(); } }; alarm(5); $th->join(); }; sub sub1 { $i = 0; while ( $i == 0 ) { } }

documentation says

if want utilize alarm time out scheme phone call need utilize eval/die pair.

alternatively can utilize sys::sigaction , below

use sys::sigaction qw( timeout_call ); if ( timeout_call( 5 ,sub { $retval = dosomething( @args ); } ) { print "dosomething() timed out\n" ; }

edit: see reply of mob question: perl threads alarm

also see give-and-take on thread safe alarms.

multithreading perl

No comments:

Post a Comment