develooper Front page | perl.ithreads | Postings from December 2008

Re: No gain in speed with threads

Thread Previous
From:
Jerry D. Hedden
Date:
December 31, 2008 07:06
Subject:
Re: No gain in speed with threads
Message ID:
1ff86f510812310706n41cf602bp5ab0004671a9c407@mail.gmail.com
Blanchette, Marco wrote:
> my @thr;
> for (my $i=0;$i<=$#ARGV;$i++){
>  push @thr, threads->new(\&extractSeq, $ARGV[$i]);
>  if (scalar(@thr) == $opt_p || $i == $#ARGV){
>    print "Running ",scalar(@thr)," parallel jobs\n";
>    $_->join for @thr;
>    undef @thr;
>  }
> }

Your job control is running in batches of $opt_p with
each batch running until all jobs are complete.  This
is inefficient.  Try the following:

foreach my $arg (@ARGV) {
    # New parallel job
    threads->new(\&extractSeq, $arg);

    # If max parallel count, then wait for some threads to finish
    while (threads->list(threads::running) >= $opt_p) {
        if (my @thrs = threads->list(threads::joinable)) {
            $_->join() foreach @thrs;
        } else {
            sleep(1);
        }
    }
}
# Wait for all running threads to finish
$_->join() foreach threads->list();

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About