#!/usr/local/bin/perl use threads; use strict; # constants my $workers = 1; #number of simultaneous workers workerPool(); sub list_threads { my @threads = threads->list; print "--threads i know of: -----\n"; for my $t (@threads) { print $t->tid."\n"; } print "--------------------------"; } # sub which maintains a set number of simultaneous threads sub workerPool { print "Starting workerPool...\n"; my $count = 0; my @running_threads = (); while (1) { @running_threads = threads->list; if (scalar(@running_threads) < $workers) { # add a new worker $count++; my $thread = threads->new(\&worker, $count); } list_threads(); } } # this sub represents a thread sub worker { my $thread_num = $_[0]; print "---I am thread $thread_num!\n"; print "+++Thread $thread_num gone!\n"; eval(((threads->self)->join)); }