Guideline: If it is an operation supported by communication, it is atomic. Otherwise, it is not.
Control:
use strict; use warnings; use feature qw( say ); use threads; use threads::shared; use constant NUM_THREADS => 4; use constant NUM_OPS => 100_000; my $q :shared = 0; my @threads; for (1..NUM_THREADS) { push @threads, async { for (1..NUM_OPS) { ++$q; } }; } $_->join for @threads; say "Got: ", $q; say "Expected: ", NUM_THREADS * NUM_OPS; say $q == NUM_THREADS * NUM_OPS ? "ok" : "fail";
Output:
Got: 163561 Expected: 400000 fail
push @a, 1; instead of ++$q :
Got: 400000 Expected: 400000 ok
ikegami
source share