The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Thread::Pool - worker thread pools to run Perl code asynchronously

SYNOPSIS

    use Thread::Pool;
    my $pool = Thread::Pool->new(Min => 5, Max => 10);
    $pool->enqueue(\&foo, @args);
    $pool->set_min(7);
    $pool->set_max(20);

DESCRIPTION

The Thread::Pool module implements pools of worker threads. Once a thread pool is created, jobs (i.e. Perl subs with arguments) may be enqueued for the pool to execute. When a thread in the pool becomes free, it takes another job from the pool's work queue and calls it. If a job is enqueued while no thread is free and the number of active threads does not exceed a given maximum, a new thread is added to the pool immediately to hande the new job. A pool can be configured to have a minimum number of threads which are always running and waiting for jobs to carry out. A pool can be configured to have a maximum number of threads - only that number of threads will ever be created in the pool and new jobs have to wait until an existing worker thread becomes free.

METHODS

new

Create a new thread pool. Arguments are optional and are of the form of key/value pairs. Passing Min => $min invokes set_min($min) (q.v.) for the pool. The default min value is zero. Passing Max => $max invokes set_max($max) (q.v.) for the pool. The default max value is -1.

enqueue(CODE [, ARG, ...])

Enqueues a new job for the pool to carry out. CODE can be a reference to a subroutine or the name of one. Unqualified subroutine names default to the caller's package. The subroutine will be called (optionally with any given arguments) in one of the worker threads in the pool either immediately (if the number of active threads in the pool has not reached its configured maximum) or when a thread becomes free (if it has reached its maximum).

set_min(MIN)

Sets the minimum number of threads in the pool to MIN. If MIN is increased then more threads are immediately created in the pool to bring the number up to MIN. When the queue of jobs for the pool is emptied, MIN worker threads will wait around for more work rather than finishing.

set_max(MAX)

Sets the maximum number of threads in the pool to MAX meaning that no more than MAX threads will ever be running concurrently for that pool. If MAX is -1 then the limit is infinite. That means that if a new job is enqueued while all threads are active, a new thread is always created immediately to handle the job.

SEE ALSO

Thread

AUTHOR

Malcolm Beattie, mbeattie@sable.ox.ac.uk.