#! perl -slw use strict; use threads; use Thread::Queue; sub worker { my $Q = shift; while( my $workItem = $Q->dequeue ) { printf "[%d] Processing workiterm '%s'\n", threads->tid, $workItem; sleep rand( 2 ); ## process $workitem } } our $WORKERS ||= 10; my $Q = new Thread::Queue; my @threads = map{ threads->create( \&worker, $Q ) } 1 .. $WORKERS; while( <> ) { ## Get workitems from stdin chomp; $Q->enqueue( $_ ); ## And queue them to the worker pool } $Q->enqueue( (undef) x $WORKERS ); ## Signal no more work $_->join for @threads; ## Wait for tehm to finish; exit; ## Done