bokan
bokan

Reputation: 3692

How to sum up all value returned by an array of threads in perl?

I have threads that execute a sub which return an integer.

sub process_tasks {
    ...
    return($nb_item_processed);
}

Here is how I launch the threads

map { threads->new(\&process_tasks, $_) } 1 .. $nb_threads;

How can I compute the sum of all value returned ?

Upvotes: 3

Views: 261

Answers (1)

ikegami
ikegami

Reputation: 385546

The value returned by a thread is returned by join.

use List::Util qw( sum );

my $sum =
   sum
      map { $_->join() }
         map { async { process_tasks($_) } }
            1 .. $nb_threads;

Upvotes: 5

Related Questions