use strict; use warnings; no warnings 'void'; use threads; use Thread::Semaphore qw( ); use Time::HiRes qw( time ); use constant NUM_THREADS => 4; my $t = time; my $start_sem = Thread::Semaphore->new( 0 ); my @barrier = map Thread::Semaphore->new( 0 ), 1 .. NUM_THREADS; my @t = map async { my $id = threads-> tid - 1; # Wait for main thread to complete. $start_sem->down; sin for 1 .. 1e7; printf "first, %d %.3f\n", $id, time - $t; # Wait for all threads to reach here. $barrier[ $id ]->up( NUM_THREADS ); $_->down for @barrier; printf "next, %d %.3f\n", $id, time - $t; }, 1 .. NUM_THREADS; sin for 1 .. 1e7; # Main thread done $start_sem->up( NUM_THREADS ); $_->join for @t;