use strict; use warnings; no warnings 'void'; use threads; use Thread::Semaphore ; use Time::HiRes 'time'; my $t = time; my @s = map Thread::Semaphore-> new( 0 ), 1 .. 4; my @t = map async { my $id = threads-> tid - 1; $s[ $id ]-> down; sin for 1 .. 1e7; # work per thread printf "first, %d %.3f\n", $id, time - $t; $s[ $id ]-> up( 4 ); $_-> down for @s; printf "next, %d %.3f\n", $id, time - $t; }, 1 .. 4; sin for 1 .. 1e7; # initial work $_-> up for @s; $_-> join for @t; __END__ first, 2 1.824 first, 3 1.824 first, 1 1.824 first, 0 1.832 next, 1 1.826 next, 3 1.828 next, 2 1.832 next, 0 1.832