| Test::SharedObject(3pm) | User Contributed Perl Documentation | Test::SharedObject(3pm) |
Test::SharedObject - Data sharing in multi process.
use strict;
use warnings;
use Test::More tests => 2;
use Test::SharedFork;
use Test::SharedObject;
my $shared = Test::SharedObject->new(0);
is $shared->get, 0;
my $pid = fork;
die $! unless defined $pid;
if ($pid == 0) {# child
$shared->txn(sub {
my $counter = shift;
$counter++;
return $counter;
});
exit;
}
wait;
is $shared->get, 1;
Test::SharedObject provides atomic data operation between multiple process.
Internally, Creates temporary file, and serialize $value by Storable, and save.
Internally:
Good Example:
$shared->txn(sub {
my $counter = shift;
$counter++; # atomic!!
return $counter;
});
Bad Example:
my $counter;
$shared->txn(sub {
$counter = shift;
});
$counter++; # *NOT* atomic!!
$shared->txn(sub {
return $counter;
});
Copyright (C) karupanerura.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
karupanerura <karupa@cpan.org>
| 2022-10-13 | perl v5.36.0 |