use threads; use threads::shared; use strict; use Time::HiRes qw/clock_gettime CLOCK_REALTIME usleep/; my $flag:shared = 1; print "starting at ".clock_gettime(CLOCK_REALTIME)."\n"; my @pool; for(1..50){ push @pool, threads->create(\&mysub); } print "giving the flag at ".clock_gettime(CLOCK_REALTIME)."\n"; $flag = 0; # all threads go! $_->join for (@pool); sub mysub { my $tid = threads->tid; while( $flag ){ print "thread $tid created and waiting for the flag at ".clock_gettime(CLOCK_REALTIME)."\n"; usleep(1000); } #my $x = 0.0001 + rand(0.0015); my $x = 0.001; #my $x = rand() >= 0.5 ? 0.001 : 1; print "thread $tid is working with sleep=$x, at ".clock_gettime(CLOCK_REALTIME)."\n"; my $id; for my $i (1..100){ $id = "i=$i, tid=$tid"; # print ` ` echo -n "start($id/\$\$) on cpu "\`cat /proc/\$\$/stat | cut -d' ' -f39\`": "; date +%s.%N sleep $x echo -n " stop($id/\$\$) on cpu "\`cat /proc/\$\$/stat | cut -d' ' -f39\`": "; date +%s.%N `; } print "ended " . threads->tid . " at ".clock_gettime(CLOCK_REALTIME)."\n"; } __END__