send/recv/begin/end 对于 AnyEvent 的 condvar 意味着什么?

发布于 2024-12-09 15:37:39 字数 540 浏览 1 评论 0原文

尽管我读过几个例子,但我不知道它的含义:

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

游魂 2024-12-16 15:37:39

在您提供的代码中, condvar 是为了防止程序过早退出。如果没有 recv,程序将在任何计时器有机会触发之前结束。对于recv,所有十个定时器都必须在recv返回之前触发。

如果从未调用过 send,则 recv 将阻塞。当调用 send 时它将解除阻塞。

beginend 是使用 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 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文