#! perl -slw use strict; use threads; use threads::shared; use List::Util qw[ shuffle ]; use Time::HiRes qw[ time ]; our $N //= 1e3; our $W //= 20; our $L //= 1; my %counts :shared; sub worker { my @order = shuffle 1 .. 20; for( 1 .. $N ) { $L and lock %counts; ++$counts{ $_ } for @order; } } my $start = time; $_->join for map threads->new( \&worker ), 1 .. $W; my $end = time; for( sort{ $a <=> $b } keys %counts ) { printf "%2u : %u\n", $_, $counts{ $_ }; } printf "Took %.6f seconds\n", $end - $start;