简体   繁体   中英

How to cleanup threads once they have finished in Perl?

I have a Perl script that launches threads while a certain expression is verified.

while ($launcher == 1) {
    # do something
    push @threads, threads ->create(\&proxy, $parameters);
    push @threads, threads ->create(\&ping, $parameters);
    push @threads, threads ->create(\&dns, $parameters);
    # more threads
    foreach (@threads) {
    $_->join();
    }
}

The first cycle runs fine but at the second one the script exits with the following error:

Thread already joined at launcher.pl line 290. Perl exited with active threads: 1 running and unjoined 0 finished and unjoined 0 running and detached

I guess I shall clean @threads but how can I do that? I am not even sure if this is the problem.

Just clear @threads at the end of the loop:

@threads = ();

Or better, declare @threads with my at the beginning of the loop:

while ($launcher == 1) {
    my @threads;

The easiest solution would be to create the array inside the while loop ( while {my @threads; ...} ), unless you need it anywhere else. Otherwise you could just @threads = () or @threads = undef at the end of the while loop.

You could also set a variable my $next_thread; outside the while loop and then assign $next_thread = @threads first thing in the while loop and change your foreach loop to

for my $index ($next_thread .. $#threads) {
    $threads[$index]->join();
}

or skip that and just loop over a slice of the last three added threads

for (@threads[-3..-1) {
    $_->join();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM