send/recv/begin/end 对于 AnyEvent 的 condvar 意味着什么?
尽管我读过几个例子,但我不知道它的含义:
#!/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;
任何人都可以更详细地解释 send/recv/begin/end
的作用吗?
更新
my $i = 1;
my $s = sub {
print $i;
};
my $i = 10;
$s->(); # 1
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您提供的代码中, condvar 是为了防止程序过早退出。如果没有
recv
,程序将在任何计时器有机会触发之前结束。对于recv
,所有十个定时器都必须在recv
返回之前触发。如果从未调用过
send
,则recv
将阻塞。当调用send
时它将解除阻塞。begin
和end
是使用send
的替代方法。当end
调用次数与begin
调用次数相同时,就会发生send
。AnyEvent
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 therecv
, all ten timers must fire beforerecv
returns.recv
will block ifsend
has never been called. It will unblock whensend
is called.begin
andend
is an alternative to usingsend
. When there has been as manyend
calls as there has beenbegin
calls, asend
occurs.AnyEvent