use 5.16.2; use threads; use Thread::Queue; my @serverList = qw(one two three four five six); # Array to hold list of servers that need to be updated my $serverQueue = Thread::Queue->new( @serverList ); # Create a Thread::Queue, passing in the array of servers as items to queue (each server is queued as a single string) $serverQueue->end; # Nothing else will be queued, so notify the queue of this by ending it my @threads = map { threads->new( \&worker ) } 1..4; # Create a suitable number of threads that wont overload IO on the local server $_->join for @threads; # Block here until all the threads have completed say 'Back to main - Finished!'; # End of program sub worker { my $tid = threads->tid; # Get the ID number of the thread while ( my $server = $serverQueue->dequeue_nb ) { # Dequeue the next item from the queue, in a non-blocking fashion (will undef and finish loop once the queue is empty) say "I'm thread ID $tid and I'm processing server $server"; sleep 1; } say "Thread ID $tid finished"; }