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
精彩评论