Threads: why locking is required when using shared variables
ikegami
created: 2006-10-19 14:47:06

I was recently faced with a thread that used .= on a shared variable, and I wondered if that was safe. I figured I'd write up a introductory tutorial on the answer I found. For simplicity, we'll look at ++ first.


The following code outputs 400,000:

my $count     = 100_000;
my $num_calls = 4;

my $sum = 0;
sub inc { ++$sum for 1..$count; }

inc() for 1..$num_calls;
print("$sum\n");   # 400000

If you ran the 4 calls to inc in parallel, would the answer still be 400,000? Not likely, if you don't change inc.

use threads;
use threads::shared;

my $count     = 100_000;
my $num_calls = 4;

my $sum : shared = 0;
sub inc { ++$sum for 1..$count; }

$_->join for map { threads->create( \&inc ) } 1..$num_calls;
print("$sum\n");   # 314813

That's because there is a [wp://race condition].

+=======================+
|          CPU          |
+-----------+-----------+
| thread 1  | thread 2  |
+===========+===========+
| ...       |           |   T
| load $sum |           |   i
| inc       |           |   m
+-----------+-----------+   e
|           | ...       |   |
|           | load $sum |   |
|           | inc       |   v
|           | save $sum |
|           | ...       |
+-----------+-----------+
| save $sum |           |
| ...       |           |
+===========+===========+

The solution is to protect the [wp://critical section] using a thread synchronization mechanism such as lock.

use threads;
use threads::shared;

my $count     = 100_000;
my $num_calls = 4;

my $sum : shared = 0;
sub inc { for (1..$count) { lock($sum); ++$sum } }

$_->join for map { threads->create( \&inc ) } 1..$num_calls;
print("$sum\n");   # 400000

Whenever an transformation operation (read ⇒ manipulate ⇒ write) is performed on a shared variable, locking is needed. See [mod://threads::shared] for tools to do this.

The program behind the below outputs results similar to the following:

++s     sum = 233564 (expecting 400000)
s+=1    sum = 143915 (expecting 400000)
c.=l    length = 248149 (expecting 400000)
c=c.l   length = 123360 (expecting 400000)

As you can see, +=, .= and = . are also not atomic. The program can only prove that an operator isn't atomic (i.e. is interruptable). It cannot prove that an operator is atomic (i.e. is not interruptable). If you're getting the "expecting" result, try upping $count and/or $threads.

use v5.8.0;

use strict;
use warnings;

use threads;
use threads::shared;


{
   my $count   = 100_000;
   my $threads = 4;

   my $sum : shared = 0;

   sub inc {
      for (1..$count) {
         ++$sum;
      }
   }

   $_->join
      for map { threads->create( \&inc ) }
          0..$threads-1;

   print("++s     sum = $sum (expecting " . ($count*$threads). ")\n");
}

{
   my $count   = 100_000;
   my $threads = 4;

   my $sum : shared = 0;

   sub inc_assign {
      for (1..$count) {
         $sum += 1;
      }
   }

   $_->join
      for map { threads->create( \&inc_assign ) }
          0..$threads-1;

   print("s+=1    sum = $sum (expecting " . ($count*$threads). ")\n");
}

{
   my $count   = 100_000;
   my $threads = 4;

   my $content : shared = '';

   sub append {
      my ($letter) = @_;
      for (1..$count) {
         $content .= $letter;
      }
   }

   $_->join
      for map { threads->create( \&append, chr(ord('a')+$_) ) }
          0..$threads-1;

   print("c.=l    length = " . length($content) .
         " (expecting " . ($count*$threads). ")\n");
}

{
   my $count   = 100_000;
   my $threads = 4;

   my $content : shared = '';

   sub concatenate {
      my ($letter) = @_;
      for (1..$count) {
         $content = $content . $letter;
      }
   }

   $_->join
      for map { threads->create( \&concatenate, chr(ord('a')+$_) ) }
          0..$threads-1;

   print("c=c.l   length = " . length($content) .
         " (expecting " . ($count*$threads). ")\n");
}

Update: Added the preface and links to Wikipedia.

Added to [Tutorials] by [planetscape] ( keep:0 edit:6 reap:0 )

Re: Threads: why locking is required when using shared variables
created: 2006-10-20 09:58:43
This is a great introduction and, certainly, locking of shared variables is essential learning and understanding for anyone who is going to be using threads.

Perhaps it is worth mentioning, in this particular case, that the lock function doesn't need an explicit unlock. Rather, a locked variable becomes unlocked when the code execution exits the current scope.

Re^2: Threads: why locking is required when using shared variables
created: 2006-10-20 13:17:46

That reminds me of a very useful bit of information reguarding locking. A single lock variable can be used to control access to multiple shared variables. For example,

my $list_head : shared;   # Access controlled by $list_head.
my $list_tail : shared;   # Access controlled by $list_head.

sub ... {
   ...
   {
      lock($list_head);
      ... code that uses $list_head and/or $list_tail ...
   }
   ...
}

It doesn't matter which variable is used as to control access to a give shared variable, as long as you *always* use the same lock variable to control access to that shared variable.

perlmonks.org content © perlmonks.org and ikegami, monarch

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03