Skip to content

Instantly share code, notes, and snippets.

@BenGoldberg1
Last active November 20, 2015 03:41
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 BenGoldberg1/bc39a9a31eaeb733012a to your computer and use it in GitHub Desktop.
Save BenGoldberg1/bc39a9a31eaeb733012a to your computer and use it in GitHub Desktop.
Bad multithreaded sort function.
#!/usr/bin/env perl6
use v6;
sub sort-promise ($c) {
my @same;
earliest $c {
more $c { push @same, $c.receive }
done $c { return }
}
my @chans = Channel.new xx 2;
my @promises = map -> $cc { start { sort-promise($cc) } }, @chans;
1 while do earliest $c {
more * {
given $_ <=> @same[0] {
when Same { push @same, $_ }
when Less { @chans[0].send( $_ ) }
when More { @chans[1].send( $_ ) }
}
True;
}
done * { False }
};
.close for @chans;
|@promises[0].result, |@same, |@promises[1].result;
}
my $sort_chan = Channel.new;
my $sort_promise = start { sort-promise($sort_chan) };
$sort_chan.send($_) for (^10).pick(*);
$sort_chan.close;
$sort_promise.result.say;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment