Coro::SemaphoreSet::try() doesn't work

SATO Kentaro kentaro at ranvis.com
Sun Feb 8 09:37:36 CET 2015


Hello,

On perl 5, version 18, subversion 2 (v5.18.2) built for
MSWin32-x86-multi-thread-64int,
Coro::SemaphoreSet::try() (Coro-6.41) doesn't work as I expected.
With the following code I expect to see any of a..c on console,
but instead everything is shown at once.

use strict;
use warnings;
use Coro;
my $ss = Coro::SemaphoreSet->new();
my @coro;
for my $i ('a'..'c') {
    push(@coro, async {
        return if (!$ss->try('res'));
        print "$i\n";
        for (;;) { cede, sleep(1) }
    });
}
$_->join() foreach (@coro);

Like Coro::SemaphoreSet::down(), I injected the code below and it
seems to do the job:

{
    use Coro::SemaphoreSet;
    no warnings;
    *Coro::SemaphoreSet::try = sub {
        $_[0][0] && Coro::Semaphore::try ($_[0][1]{$_[1]} ||=
Coro::Semaphore::_alloc $_[0][0]);
    };
}

Regards,

Kentaro SATO



More information about the perl mailing list