1. Scalars
Thread1 Thread2
$a = 1; $a = 2;
$a is set to 1 and then 2, or 2 and then 1. It just depends which
thread gets there first.
2. Arrays
Thread1 Thread2
push @a, (1, 2, 3); push @a, (4, 5, 6);
The interesting question is whether push is atomic.
Atomic
@a is either (1, 2, 3, 4, 5, 6) or (4, 5, 6, 1, 2, 3)
Non-atomic
@a could be (1, 4, 2, 5, 3, 6)
Users can probably live with either; the hard problems are
implementation under SMP. If push is atomic, then we lock out one
processor for a potentially long time while the other processor
executes the push. If push isn't atomic, then we enter and leave a
critical section once for each element that is pushed.
3. Subroutines
sub foo { print "foo" }
Thread1 Thread2
foo(); eval 'sub foo { print "bar" }';
This prints either "foo" or "bar".
Thread2 replaces the coderef for &main::foo. This could happen *while*
Thread1 is executing foo(). We can handle this by having Thread1 hold
a reference count on the coderef while it is executing foo().
4. Regular expressions
Thread1 Thread2
$a =~ /.../ $a = 'foo';
It seems like an RE match should be atomic, because it hard to imagine
it executing sensibly if the target string changes during the match.
But then we have
$a =~ s/.../.../e; $a = 'foo';
s///e can execute arbitrary code, so if s// is atomic, then Thread2
could be blocked for an arbitrary length of time.
- SWM