Last active
December 13, 2015 22:59
-
-
Save zigorou/4988679 to your computer and use it in GitHub Desktop.
Coro
This file contains hidden or 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 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"; |
This file contains hidden or 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 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"; |
This file contains hidden or 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 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"; |
This file contains hidden or 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 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