#! perl -slw use strict; use threads ( stack_size => 4096 ); use threads::shared; use Thread::Queue; sub process { my $tid = threads->tid; my( $hashref, $Q ) = @_; while( my $key = $Q->dequeue ) { print "$tid : Processing $key"; sleep 10; lock $hashref; ++$hashref->{ $key }; } } our $T //= 16; my %hash :shared; my $Q = new Thread::Queue; my @t = map threads->create( \&process, \%hash, $Q ), 1 .. $T; while( my $key = <> ) { chomp $key; if( exists $hash{ $key } ) { $Q->enqueue( $key ); } else { $hash{ $key } = 0; } } $Q->enqueue( (undef) x $T ); $_->join for @t;