<?xml version="1.0" encoding="windows-1252"?>
<node id="579444" title="Threads: why locking is required when using shared variables" created="2006-10-19 14:47:06" updated="2006-10-19 10:47:06">
<type id="956">
perltutorial</type>
<author id="381608">
ikegami</author>
<data>
<field name="doctext">
&lt;p&gt;I was recently faced with a thread that used &lt;c&gt;.=&lt;/c&gt; 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 &lt;c&gt;++&lt;/c&gt; first.

&lt;hr&gt;

&lt;p&gt;The following code outputs 400,000:

&lt;c&gt;
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
&lt;/c&gt;

&lt;p&gt;If you ran the 4 calls to &lt;c&gt;inc&lt;/c&gt; in parallel, would the answer still be 400,000? Not likely, if you don't change &lt;c&gt;inc&lt;/c&gt;.

&lt;c&gt;
use threads;
use threads::shared;

my $count     = 100_000;
my $num_calls = 4;

my $sum : shared = 0;
sub inc { ++$sum for 1..$count; }

$_-&gt;join for map { threads-&gt;create( \&amp;inc ) } 1..$num_calls;
print("$sum\n");   # 314813
&lt;/c&gt;

&lt;p&gt;That's because there is a [wp://race condition].

&lt;c&gt;
+=======================+
|          CPU          |
+-----------+-----------+
| thread 1  | thread 2  |
+===========+===========+
| ...       |           |   T
| load $sum |           |   i
| inc       |           |   m
+-----------+-----------+   e
|           | ...       |   |
|           | load $sum |   |
|           | inc       |   v
|           | save $sum |
|           | ...       |
+-----------+-----------+
| save $sum |           |
| ...       |           |
+===========+===========+
&lt;/c&gt;

&lt;p&gt;The solution is to protect the [wp://critical section] using a thread synchronization mechanism such as &lt;c&gt;lock&lt;/c&gt;.

&lt;c&gt;
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 } }

$_-&gt;join for map { threads-&gt;create( \&amp;inc ) } 1..$num_calls;
print("$sum\n");   # 400000
&lt;/c&gt;

&lt;p&gt;Whenever an transformation operation (read &amp;#8658; manipulate &amp;#8658; write) is performed on a shared variable, locking is needed. See [mod://threads::shared] for tools to do this.

&lt;p&gt;The program behind the &lt;c&gt;&lt;spoiler&gt;&lt;/c&gt; below outputs results similar to the following:

&lt;c&gt;
++s     sum = 233564 (expecting 400000)
s+=1    sum = 143915 (expecting 400000)
c.=l    length = 248149 (expecting 400000)
c=c.l   length = 123360 (expecting 400000)
&lt;/c&gt;

&lt;p&gt;As you can see, &lt;c&gt;+=&lt;/c&gt;, &lt;c&gt;.=&lt;/c&gt; and &lt;c&gt;= .&lt;/c&gt; 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 &lt;c&gt;$count&lt;/c&gt; and/or &lt;c&gt;$threads&lt;/c&gt;.

&lt;spoiler&gt;
&lt;c&gt;
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;
      }
   }

   $_-&gt;join
      for map { threads-&gt;create( \&amp;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;
      }
   }

   $_-&gt;join
      for map { threads-&gt;create( \&amp;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;
      }
   }

   $_-&gt;join
      for map { threads-&gt;create( \&amp;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;
      }
   }

   $_-&gt;join
      for map { threads-&gt;create( \&amp;concatenate, chr(ord('a')+$_) ) }
          0..$threads-1;

   print("c=c.l   length = " . length($content) .
         " (expecting " . ($count*$threads). ")\n");
}
&lt;/c&gt;
&lt;/spoiler&gt;

&lt;p&gt;&lt;b&gt;Update&lt;/b&gt;: Added the preface and links to Wikipedia.

&lt;p&gt;&lt;small&gt;Added to [Tutorials] by [planetscape] &lt;readmore title="view votes"&gt;( keep:0 edit:6 reap:0 )&lt;/readmore&gt;&lt;/small&gt;&lt;/p&gt;
</field>
</data>
</node>
