in reply to How to terminate threads that hang or never return?
For a thread to be joinable, it must "return" from it's code block with a return statement, OR reach the end of the code block.
Since you didn't show how you wrote &somefunction, I can only generalize.
or maybe alarm?sub somefunction{ # somehow incorporate an alarm or a loop # to run a "return" or "goto END" ....... if ( $shared_flag ==1 ){ return } #or if( $shared_flag == 1 ){ goto END } ...... ...... END: }
You can also try to detach the thread, and have it set a shared variable when it successfully completes. Then loop through the shared flags at the end of your main thread, and kill hanging threads.sub somefunction{ local $SIG{ALRM} = sub { goto END }; alarm 10; # wait 10 seconds before interrupting while (1){sleep} END: }
|
|---|