use strict; use threads; use Thread::Queue; my ( $cnt, $_th_count_parse, $_queue_parse, $_queue_qualify, @th ); # get time of start use win32; my ($runtime) = Win32::GetTickCount(); # feed worker for ( my $i = 0 ; $i < 10000 ; $i++ ) { # read from file and put in $_ here my $dummy = 'dummy'; $cnt++; $_queue_parse->enqueue( [ $cnt, \$dummy ] ); } # send command: stop for every thread $_queue_parse->enqueue( [ -1, 0 ] ); $_queue_parse->enqueue( [ -1, 0 ] ); # recieve answer RecieveAnswer(); # join threads $th[0]->join(); $th[1]->join(); $runtime = ( Win32::GetTickCount() - $runtime ) / 1000; print "Zeit: $runtime sec\n"; # sub RecieveAnswer { my $thclosed; while ( my $temp = $_queue_qualify->dequeue ) { if ( $$temp{'queue_id'} == -1 ) { $thclosed++; } if ( $thclosed == 2 ) { return 0 } # do work here } } # mock-sub for producing a hash sub ParseDok { my %hash; for ( my $i = int( rand(20) ) ; $i < 100 ; $i++ ) { $hash{ $i * 100 + int( rand(100) ) } = 'Test' . $i; } return \%hash; } # thread handler sub handle_thread_parser { my $whoiam = shift; my $wq = shift; my $qq = shift; my ( $temp, $element, $wrkcnt ); # work loop while ( $element = $wq->dequeue ) { # work finished? if ( $$element[0] == -1 ) { my $t = { "queue_id" => -1 }; $qq->enqueue($t); print "Thread $whoiam closing on command with workload $wrkcnt\n"; return 0; } # do work $wrkcnt++; $temp = ParseDok( ${ $$element[1] } ) || ''; $$temp{'queue_id'} = $$element[0]; # deliver $qq->enqueue($temp); } } # setup worker threads BEGIN { $_th_count_parse = 2; # Queues $_queue_parse = Thread::Queue->new; $_queue_qualify = Thread::Queue->new; # go! for ( my $i = 0 ; $i < $_th_count_parse ; $i++ ) { $th[$i] = threads->new( \&handle_thread_parser, $i, $_queue_parse, $_queue_qualify ); }