NAME

threads::farm - group of threads for performing similar jobs

SYNOPSIS

    use threads::farm;
    $farm = threads::farm->new(
     {
      autoshutdown => 1, # default: 1 = yes
      workers => 5,      # default: 1
      pre => sub {shift; print "starting worker with @_\n",
      do => sub {shift; print "doing job for @_\n"; reverse @_},
      post => sub {shift; print "stopping worker with @_\n",
     },
     qw(a b c)           # parameters to pre-job subroutine
    );

    $farm->job( qw(d e f) );              # not interested in result

    $jobid = $farm->job( qw(g h i) );
    @result = $farm->result( $jobid );    # wait for result to be ready
    print "Result is @result\n";

    $jobid = $farm->job( qw(j k l) );
    @result = $farm->result_nb( $jobid ); # do _not_ wait for result
    print "Result is @result\n";          # may be empty when not ready yet

    $farm->hire;          # add worker(s)
    $farm->fire;          # remove worker(s)
    $farm->workers( 10 ); # set number of workers

    $hired = $farm->hired; 
    $fired = $farm->fired;
    print "$hired workers hired, $fired workers fired\n";
    
    $todo = $farm->todo;
    $done = $farm->done;
    print "$done jobs done, still $todo jobs todo\n";

    $farm->autoshutdown( 1 ); # shutdown when object is destroyed
    $farm->shutdown;          # wait until all jobs done

    @pre = shift->pre; # inside do() and post() only;

DESCRIPTION

METHODS

new
job
result
result_nb
todo
done
hire
fire
workers
hired
fired
autoshutdown
shutdown
pre

CAVEATS

Passing unshared values between threads is accomplished by serializing the specified values using Storable. This allows for great flexibility at the expense of more CPU usage. It also limits what can be passed, as e.g. code references can not be serialized and therefor not be passed.

SEE ALSO

threads, Thread::Queue::Any, Storable.