Acme-Parataxis
view release on metacpan or search on metacpan
lib/Acme/Parataxis/Sync/WaitGroup.pod view on Meta::CPAN
=pod
=encoding utf8
=head1 NAME
Acme::Parataxis::Sync::WaitGroup - Coordinate Completion of Multiple Concurrent Fibers
=head1 SYNOPSIS
use v5.40;
use Acme::Parataxis qw[async fiber await_sleep];
use Acme::Parataxis::Sync::WaitGroup;
my $wg = Acme::Parataxis::Sync::WaitGroup->new;
async {
# Register 3 tasks before spawning:
$wg->add(3);
for my $id ( 1 .. 3 ) {
fiber {
say "Task $id running...";
await_sleep(50);
$wg->done; # decrement counter
};
}
say "Waiting for all tasks to finish...";
$wg->wait; # parks until counter hits 0
say "All tasks complete!";
};
=head1 DESCRIPTION
An C<Acme::Parataxis::Sync::WaitGroup> coordinates the completion of multiple asynchronous tasks, providing the fan-in
half of a fan-out/fan-in concurrency pattern.
The group maintains an integer counter:
=over
=item * C<add($n)> increments the counter by C<$n>.
=item * C<done()> decrements the counter by C<1>.
=item * C<wait()> parks the calling fiber until the counter drops to C<0>.
=back
WaitGroups do not track fibers or job handles; they track a numeric tally. This keeps the primitive lightweight and
fast, but requires callers to ensure that every C<add> is matched by exactly one C<done>.
=head1 METHODS
=head2 C<new( [ count =E<gt> $n = 0 ] )>
my $wg = Acme::Parataxis::Sync::WaitGroup->new;
my $primed = Acme::Parataxis::Sync::WaitGroup->new( count => 5 );
Constructs a new WaitGroup with an initial counter of C<$n> (default C<0>).
=head2 C<add( $n )>
( run in 1.151 second using v1.01-cache-2.11-cpan-234fa220139 )