Last active
November 20, 2015 03:41
-
-
Save BenGoldberg1/bc39a9a31eaeb733012a to your computer and use it in GitHub Desktop.
Bad multithreaded sort function.
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
#!/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