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