… | |
… | |
576 | after => 1, |
576 | after => 1, |
577 | cb => sub { $result_ready->send }, |
577 | cb => sub { $result_ready->send }, |
578 | ); |
578 | ); |
579 | |
579 | |
580 | # this "blocks" (while handling events) till the callback |
580 | # this "blocks" (while handling events) till the callback |
581 | # calls -<send |
581 | # calls ->send |
582 | $result_ready->recv; |
582 | $result_ready->recv; |
583 | |
583 | |
584 | Example: wait for a timer, but take advantage of the fact that condition |
584 | Example: wait for a timer, but take advantage of the fact that condition |
585 | variables are also callable directly. |
585 | variables are also callable directly. |
586 | |
586 | |
… | |
… | |
643 | into one. For example, a function that pings many hosts in parallel |
643 | into one. For example, a function that pings many hosts in parallel |
644 | might want to use a condition variable for the whole process. |
644 | might want to use a condition variable for the whole process. |
645 | |
645 | |
646 | Every call to "->begin" will increment a counter, and every call to |
646 | Every call to "->begin" will increment a counter, and every call to |
647 | "->end" will decrement it. If the counter reaches 0 in "->end", the |
647 | "->end" will decrement it. If the counter reaches 0 in "->end", the |
648 | (last) callback passed to "begin" will be executed. That callback is |
648 | (last) callback passed to "begin" will be executed, passing the |
649 | *supposed* to call "->send", but that is not required. If no |
649 | condvar as first argument. That callback is *supposed* to call |
|
|
650 | "->send", but that is not required. If no group callback was set, |
650 | callback was set, "send" will be called without any arguments. |
651 | "send" will be called without any arguments. |
651 | |
652 | |
652 | You can think of "$cv->send" giving you an OR condition (one call |
653 | You can think of "$cv->send" giving you an OR condition (one call |
653 | sends), while "$cv->begin" and "$cv->end" giving you an AND |
654 | sends), while "$cv->begin" and "$cv->end" giving you an AND |
654 | condition (all "begin" calls must be "end"'ed before the condvar |
655 | condition (all "begin" calls must be "end"'ed before the condvar |
655 | sends). |
656 | sends). |
… | |
… | |
683 | that are begung can potentially be zero: |
684 | that are begung can potentially be zero: |
684 | |
685 | |
685 | my $cv = AnyEvent->condvar; |
686 | my $cv = AnyEvent->condvar; |
686 | |
687 | |
687 | my %result; |
688 | my %result; |
688 | $cv->begin (sub { $cv->send (\%result) }); |
689 | $cv->begin (sub { shift->send (\%result) }); |
689 | |
690 | |
690 | for my $host (@list_of_hosts) { |
691 | for my $host (@list_of_hosts) { |
691 | $cv->begin; |
692 | $cv->begin; |
692 | ping_host_then_call_callback $host, sub { |
693 | ping_host_then_call_callback $host, sub { |
693 | $result{$host} = ...; |
694 | $result{$host} = ...; |