… | |
… | |
666 | one. For example, a function that pings many hosts in parallel might want |
666 | one. For example, a function that pings many hosts in parallel might want |
667 | to use a condition variable for the whole process. |
667 | to use a condition variable for the whole process. |
668 | |
668 | |
669 | Every call to C<< ->begin >> will increment a counter, and every call to |
669 | Every call to C<< ->begin >> will increment a counter, and every call to |
670 | C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end |
670 | C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end |
671 | >>, the (last) callback passed to C<begin> will be executed. That callback |
671 | >>, the (last) callback passed to C<begin> will be executed, passing the |
672 | is I<supposed> to call C<< ->send >>, but that is not required. If no |
672 | condvar as first argument. That callback is I<supposed> to call C<< ->send |
673 | callback was set, C<send> will be called without any arguments. |
673 | >>, but that is not required. If no group callback was set, C<send> will |
|
|
674 | be called without any arguments. |
674 | |
675 | |
675 | You can think of C<< $cv->send >> giving you an OR condition (one call |
676 | You can think of C<< $cv->send >> giving you an OR condition (one call |
676 | sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND |
677 | sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND |
677 | condition (all C<begin> calls must be C<end>'ed before the condvar sends). |
678 | condition (all C<begin> calls must be C<end>'ed before the condvar sends). |
678 | |
679 | |
… | |
… | |
705 | begung can potentially be zero: |
706 | begung can potentially be zero: |
706 | |
707 | |
707 | my $cv = AnyEvent->condvar; |
708 | my $cv = AnyEvent->condvar; |
708 | |
709 | |
709 | my %result; |
710 | my %result; |
710 | $cv->begin (sub { $cv->send (\%result) }); |
711 | $cv->begin (sub { shift->send (\%result) }); |
711 | |
712 | |
712 | for my $host (@list_of_hosts) { |
713 | for my $host (@list_of_hosts) { |
713 | $cv->begin; |
714 | $cv->begin; |
714 | ping_host_then_call_callback $host, sub { |
715 | ping_host_then_call_callback $host, sub { |
715 | $result{$host} = ...; |
716 | $result{$host} = ...; |