Data-Pool-Shared

 view release on metacpan or  search on metacpan

eg/atomic_counters.pl  view on Meta::CPAN


my $nworkers = shift || 4;
my $iters    = shift || 100_000;

my $pool = Data::Pool::Shared::I64->new(undef, 4);

# allocate 4 counters
my @counters = map { $pool->alloc } 1..4;
$pool->set($_, 0) for @counters;

printf "%d workers x %d iterations on %d counters\n",
    $nworkers, $iters, scalar @counters;

my $t0 = time;
my @pids;
for my $w (1..$nworkers) {
    my $pid = fork // die "fork: $!";
    if ($pid == 0) {
        for (1..$iters) {
            $pool->add($counters[$_ % 4], 1) for 0..3;
        }



( run in 1.201 second using v1.01-cache-2.11-cpan-71847e10f99 )