What built-in perl functions are atomic?

I am writing some kind of streaming code, and I'm curious that Perl, built into functions and operators, is atomic and safe to use in a shared variable without blocking. For example, they told me ++ , -- , += and the like, because they are implemented as two operations.

Is there a list somewhere? In particular, push , pop , shift , unshift and splice on an atom with a common array:

Thanks.

+8
multithreading perl
source share
1 answer

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 
+6
source share

All Articles