my $checkpoint = Supplier.new; | |
my $total-workers = 5; | |
my $units-to-build = 3; | |
await (1..$total-workers).map: { | |
start { | |
my $id = $_; | |
my %who's-ready; | |
my $go = Channel.new; | |
$checkpoint.Supply.tap: { | |
die "Oh no, something's wrong!" if %who's-ready{$_}:exists; | |
%who's-ready{$_} = True; | |
if %who's-ready == $total-workers { | |
%who's-ready{*}:delete; | |
$go.send: Any; | |
} | |
}; | |
for ^$units-to-build { | |
my $duration = 3.rand; | |
say "$id starts working for $duration seconds."; | |
sleep $duration; | |
say "$id is done, waiting."; | |
$checkpoint.emit: $id; | |
$go.receive; | |
} | |
} | |
} | |
say "All work is done!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment