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.

Answers


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;

Need Your Help

Draw text with a stroke onto an image in .Net

.net graphics text gdi

I am currently using the System.Drawing.Graphics.DrawString() method in .Net to draw text on top of an image, and then save it to a new file:

Prevent ComboBox to expand acording to selected item

c# wpf layout

I want to prevent a ComboBox to resize acording to the size of the selected item. Consider this simplified example: