#! perl -slw use strict; use threads; use threads::shared; use Thread::Queue; use Time::HiRes qw[ time sleep ]; use constant { THREADS => 10, TRAPS => 1000, }; my $Q = new Thread::Queue; my $start : shared = 0; my $running : shared = 0; sub thread { $running++; sleep 0.01 until $start; while( my $trap = $Q->dequeue ) { ## Simulate sending trap. sleep $trap; } $running--; } my @threads = map{ threads->create( \&thread ) } 1 .. THREADS; ## Random "transmit" times 10 to 40 milliseconds $Q->enqueue ( (10 + rand 40) / 1000 ) for 1 .. TRAPS; ## one undef per thread to terminate their while loops $Q->enqueue( (undef) x THREADS ); ## until they are all running sleep .1 until $running == THREADS; ## Get the start time and set them going my $startTime = time; $start = 1; ## Wait for the queue to empty sleep .01 while $Q->pending; ## Wait till the've all stopped sleep .01 while $running; ## Grab the end time my $endTime = time; print "1000 traps took and average of ", ( $endTime - $startTime ) / 1000, " seconds per trap"; __END__ P:\test>junk3 1000 traps took and average of 0.00372980403900147 seconds per trap