user3547407
user3547407

Reputation: 187

Perl how to kill 'rogue' thread in perl while continuing other threads

I'm creating a perl application which executes in multiple threads and each thread consuming time. This is what I have so far

use strict;
use warnings;
use threads;

my @file_list = ("file1", "file2", "file3");
my @jobs;
my @failed_jobs;
my $timeout = 10; #10 seconds timeout

foreach my $s (@file_list){
    push @jobs, threads->create(sub{
        #time consuming task
    })
}

$_->join for @jobs;

The problem is that the time consuming task may sometimes get stuck (or take more than $timeout seconds of time to run). So when that happens, I want to get the name of the file and push it to @failed_jobs and then kill that thread. However, I want to continue with the other threads. When all threads are either killed or completed, I want to exit.

Can someone tell me how to modify my above code to achieve this?

Thanks

Upvotes: 1

Views: 69

Answers (1)

ikegami
ikegami

Reputation: 385556

If you want the ability to kill the task, you don't want threads but processes.

Upvotes: 1

Related Questions