CJ7
CJ7

Reputation: 23275

Simple way to use concurrent threads in perl?

sub test {
   $val = shift;
   print $val * 2;
}

for $i (1..1000)
{
   test($i);
}

How can I get the calls to test to be executed in concurrent threads using Thread::Pool or Thread::Pool::Simple ?

Upvotes: 2

Views: 93

Answers (1)

CJ7
CJ7

Reputation: 23275

use feature 'say';
use Thread::Pool::Simple;

sub test
{
    say shift;
    sleep(5);
}


my $pool = Thread::Pool::Simple->new (

    do => [\&test],

    min => 10,
    max => 20


);

$pool->add($_) for (1..50);
$pool->join;
say "Finished";

Upvotes: 2

Related Questions