in reply to Threads Timeout
You could try this:
#! perl -slw use strict; use threads; my @t = map async( sub { my $tid = threads->tid; my $worktime = shift; print "Thread $tid started with a worktime of $worktime"; eval { local $SIG{ ALRM } = sub { die 'times up' }; alarm 10; ## simulate doing work here sleep $worktime; print "Thread $tid: all done"; }; print "Thread $tid ended ", $@ ? " with error: $@" : 'normally'; }, 2*$_ + $ARGV[ 0 ] ), 1 .. 4; $_->join for @t; __END__ C:\test>junk78 5 Thread 1 started with a worktime of 7 Thread 2 started with a worktime of 9 Thread 3 started with a worktime of 11 Thread 4 started with a worktime of 13 Thread 1: all done Thread 1 ended normally Thread 2: all done Thread 2 ended normally Thread 4 ended with error: times up at C:\test\junk78.pl line 10. Thread 3 ended with error: times up at C:\test\junk78.pl line 10.
Whether this works -- ie. will interrupt what you are doing when the alarm goes off is subject to the same limitations as with normal perl.
Due to Deferred (safe) signals introduced in 5.7.x, there are some things it won't interrupt until the opcode completes or times out. But that's a 'Perl-thing' rather than a 'threads-thing'.
|
|---|