Simple parallel processing in perl

I have several blocks of code inside the function of some object that can work in parallel and speed up work for me.

I tried using subs::parallelas follows (all this in the function body):

my $is_a_done = parallelize { 
                              # block a, do some work
                              return 1;
                             };
my $is_b_done = parallelize { 
                              # block b, do some work
                              return 1;
                             };
my $is_c_done = parallelize { 
                              # block c depends on a so let wait (block)
                              if ($is_a_done) {
                               # do some work
                              };
                              return 1;
                             };
my $is_d_done = parallelize { 
                              # block d, do some work
                              return 1;
                             };

if ($is_a_done && $is_b_done && $is_c_done && $is_d_done) {
 # just wait for all to finish before the function returns
}

First, note that I use ifto wait until threads block and wait for the previous thread to finish when it is needed (best idea? ifPretty ugly ...).

Secondly, I get an error message:

Thread already joined at /usr/local/share/perl/5.10.1/subs/parallel.pm line 259.
Perl exited with active threads:
    1 running and unjoined
    -1 finished and unjoined
    3 running and detached
+5
source share
1 answer

subs::parallel, , , , , , , .

, , , , ? .

use threads;
my @jobs;
push @jobs, threads->create(sub {
  # do some work
});

push @jobs, threads->create(sub {
  # do some other work
});

# Repeat as necessary :)

$_->join for @jobs; # Wait for everything to finish.

- , ( ), , , , .

+13

All Articles