#! perl -slw use strict; use threads; use threads::shared; $|=1; our $N ||= 1_000; my $threads :shared = 0; for my $i ( 1 .. $N ) { ## if the condition is true if( $i & 1 ) { ## every odd number matches ## Start a thread to run the sub ## Passing the number as an argument ## And saving the thread object, async( \&other, $i )->detach; } } ## Wait for all the threads to finish sleep 1 while $threads; printf "Check Memory"; ; ## Done. exit; sub other{ { lock $threads; ++$threads; } my( $number ) = @_; print "Other( $number ) starting"; ## pretend we've doing something that takes a while... for( 1 .. rand 100 ) { print "Other( $number ) busy ..."; Win32::Sleep 100; } print "Other( $number ) finished"; { lock $threads; --$threads; } }