-
-
Save jkramer/7e04b7c93ac905f179b9d33651e70de6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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