开发者

How can I instantly kill a Perl thread without killing the entire program?

开发者 https://www.devze.com 2023-03-27 10:28 出处:网络
When I use exit or die, it kills the entire program. foreach my $t (threads->list()) { $t->exit; $count++;

When I use exit or die, it kills the entire program.

foreach my $t (threads->list())
{
    $t->exit;
    $count++;
}

Usage: threads->exit(status) at main.pl line 265
Perl exited with active threads:
        9 running and unjoined
        0 finished and unjoined
        0 running and detached
开发者_C百科

Any ideas?


To ignore a thread that is being executed, return back control and throw away whatever it may output, the right method to use is detach, not exit.

See perldoc perlthrtut - Ignoring a Thread.


perldoc threads explains why the code exits:

threads->exit()

If needed, a thread can be exited at any time by calling threads->exit(). This will cause the thread to return undef in a scalar context, or the empty list in a list context. When called from the main thread, this behaves the same as exit(0).


There may be a way to achieve instant termination (didn't work for me on Windows):

use threads 'exit' => 'threads_only';

This globally overrides the default behavior of calling exit() inside a thread, and effectively causes such calls to behave the same as threads->exit() . In other words, with this setting, calling exit() causes only the thread to terminate. Because of its global effect, this setting should not be used inside modules or the like. The main thread is unaffected by this setting.

The documentation also suggests another way, using the set_thread_exit_only method (Again, didn't work for me on Windows):

$thr->set_thread_exit_only(boolean)

This can be used to change the exit thread only behavior for a thread after it has been created. With a true argument, exit() will cause only the thread to exit. With a false argument, exit() will terminate the application. The main thread is unaffected by this call.


The example below makes use of a kill signal to terminate the $unwanted thread:

use strict;
use warnings;
use threads;

my $unwanted = threads->create( sub {
                                      local $SIG{KILL} = sub { threads->exit };
                                      sleep 5;
                                      print "Don't print me!\n";
                                    } );

my $valid    = threads->create( sub {
                                      sleep 2;
                                      print "This will print!\n";
                                    } );

$unwanted->kill('KILL')->detach;   # Kills $thr, cleans up

$valid->join;                 # sleep 2, 'This will print!'


If you want to kill a thread, you kill it with $thread->kill. If you want to disown a thread but leave it running, use $thread->detach. threads->exit causes the current thread to exit; it doesn't take a thread as invocant.


The error message is telling you that $t->exit requires an argument. Try giving it one. (perldoc Threads seems to say that it doesn't, but I don't know what package you're using.)

0

精彩评论

暂无评论...
验证码 换一张
取 消