Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active August 29, 2015 14:13
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 ab5tract/4c2b8cc439d8470cbfda to your computer and use it in GitHub Desktop.
Save ab5tract/4c2b8cc439d8470cbfda to your computer and use it in GitHub Desktop.
perl 5 + OO::Monitors
$ perl6 thread-p5.pl6
===SORRY!===
Cannot look up attributes in a type object
#!/usr/bin/env perl6
use OO::Monitors;
use Inline::Perl5;
monitor Perl5::Pool is conditioned( <interpreters-available> ) {
has @!pool;
has SetHash $!pool-mask;
has $.pool-size;
# method new(%args) {
# my $pool-size = %args<pool-size> // 5;
# my @pool;
# @pool[$_] = Inline::Perl5.new for 0..^$!pool-size;
# %args.perl.say;
# my @pool;
# self.bless( :$pool-size, :@pool );
# }
method interpret( :$perl5-code ) {
unless +@!pool {
@!pool[$_] = Inline::Perl5.new for 0..^$!pool-size;
}
while @!pool.keys.Set !(>) $!pool-mask {
wait-condition(<interpreters-available>);
}
meet-condition(<interpreters-available>);
my $idx = @!pool.keys.first({ $!pool-mask !(cont) $_ });
say $idx;
my $interpreter = @!pool[ $idx ];
until my $response = $interpreter.run( $perl5-code ) {
$!pool-mask{$idx} = 1;
}
return $response;
}
}
my $pool = Perl5::Pool.new( :pool-size(8) );
my @results = await do for ^8 -> $thread {
start {
my $perl5-code = "sleep { (0..0.5).pick }; print 'hello from thread $thread';";
say $pool.interpret( :$perl5-code );
$thread;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment