Skip to content

Instantly share code, notes, and snippets.

@zigorou
Last active December 13, 2015 22:59
Show Gist options
  • Save zigorou/4988679 to your computer and use it in GitHub Desktop.
Save zigorou/4988679 to your computer and use it in GitHub Desktop.
Coro
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use Coro;
async {
my @args = @_;
my $rv = 1;
for (@args) {
$rv *= $_;
}
say $rv;
} (1,2,3);
cede;
say "finish";
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use Coro;
my $coro = Coro->new(sub {
my @args = @_;
my $rv = 1;
for (@args) {
$rv *= $_;
}
say $rv;
}, (1,2,3));
$coro->ready;
cede;
say "finish";
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use Coro;
use Data::Dump qw(dump);
use Scalar::Util qw(refaddr);
my $coro = Coro->new(sub {
my @args = @_;
my $rv = 1;
for (@args) {
$rv *= $_;
}
say $rv;
say dump($Coro::current, refaddr $Coro::current);
}, (1,2,3));
say dump($coro, refaddr $coro);
$coro->ready;
$Coro::current->ready;
say dump($Coro::current, refaddr $Coro::current);
Coro::schedule;
say dump($Coro::current, refaddr $Coro::current);
say "finish";
#!/usr/bin/env perl
use strict;
use warnings;
use Coro;
use Coro::Semaphore;
async {
# some asynchronous thread of execution
print "2\n";
cede; # yield back to main
print "4\n";
};
print "1\n";
cede; # yield to coro
print "3\n";
cede; # and again
# use locking
my $lock = Coro::Semaphore->new;
my $locked;
$lock->down;
$locked = 1;
$lock->up;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment