in reply to Locked threads and tcp timeouts
You might try the following, and if that dosn't work, because of the needed SIGINT, the only sure fire way I know would be to fork off the code that can block, run a timer on it, and kill -9 it's $pid when the timer expires.
#!/usr/bin/perl -w use strict; use threads; use threads::shared; my $timer_go:shared = 0; my $worker = threads->create(\&worker); my $timer = threads->create(\&timer,$worker)->detach(); print "hit enter to start\n"; <>; $timer_go=1; while( (scalar threads->list) > 0 ){ print scalar threads->list,"\n"; sleep 1; foreach my $thread (threads->list) { if( $thread->is_joinable ){ $thread->join;} } } print "worker joined, all done\n"; exit; sub timer { my $worker = shift; while(1){ if($timer_go){ my $count = 0; while(1){ $count++; if($count > 5){ print "timed out\n"; # Send a signal to a thread $worker->kill('INT'); return; # will destroy a detached thread } sleep 1; print "timing $count\n"; } }else{sleep 1} } } sub worker { $|++; $SIG{INT} = sub{ warn "Caught Zap!\n"; threads->exit() }; # threads->exit() will exit thread only while(1){sleep 1; next} return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Locked threads and tcp timeouts
by BrowserUk (Patriarch) on Feb 16, 2012 at 17:05 UTC | |
by nikosv (Deacon) on Feb 19, 2012 at 15:42 UTC | |
by BrowserUk (Patriarch) on Feb 20, 2012 at 02:50 UTC |