Skip to content

Instantly share code, notes, and snippets.

@jkramer
Created June 29, 2018 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkramer/7e04b7c93ac905f179b9d33651e70de6 to your computer and use it in GitHub Desktop.
Save jkramer/7e04b7c93ac905f179b9d33651e70de6 to your computer and use it in GitHub Desktop.
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