开发者

What does send/recv/begin/end mean for AnyEvent's condvar?

开发者 https://www.devze.com 2023-04-13 02:33 出处:网络
I\'m at a loss what it means, though I\'m read several examples on it: #!/usr/bin/perl use strict; use AnyEvent;

I'm at a loss what it means, though I'm read several examples on it:

#!/usr/bin/perl
use strict;
use AnyEvent;

my $cv = AnyEvent->condvar( cb => sub {
    warn "done";
});

for my $i (1..10) {
    $cv->begin;
    my $w; $w = AnyEvent->timer(after => $i, cb => sub {
    warn "finished timer $i";
    undef $w;
    $cv->end;
    });
}

$cv->recv;

Can anyone explain 开发者_开发技巧in more detail what send/recv/begin/end does?

UPDATE

my $i = 1;
my $s = sub {
    print $i;
};
my $i = 10;
$s->();  # 1


In the code you provided, the condvar is there to prevent the program from exiting prematurely. Without the recv, the program would end before any timers would have a chance to fire. With the recv, all ten timers must fire before recv returns.

recv will block if send has never been called. It will unblock when send is called.

begin and end is an alternative to using send. When there has been as many end calls as there has been begin calls, a send occurs.

AnyEvent

0

精彩评论

暂无评论...
验证码 换一张
取 消