Locks and Interlocked

Two threads that add one to the same counter do not always add two. The increment is a read, an addition and a write, and two threads can read before either writes. This lesson makes that overlap happen on purpose so that the result is reproducible, then fixes it twice: with lock and with Interlocked.

A lost update

A real race depends on the scheduler and gives a different number on every run. The program below forces one with two events: the first thread reads the counter and waits, the second reads the same value and lets the first write, and then both write their copy plus one:

var counter = 0; var read = new ManualResetEventSlim(); var written = new ManualResetEventSlim(); var first = new Thread(() => { var copy = counter; read.Set(); written.Wait(); counter = copy + 1; }); var second = new Thread(() => { read.Wait(); var copy = counter; written.Set(); counter = copy + 1; }); first.Start(); second.Start(); first.Join(); second.Join(); Console.WriteLine($"lost update {counter}");

dotnet run

lost update 1

Two increments ran and the counter is one, because both read zero and both wrote one. Nothing about this is unusual for a machine: a thread can be paused between any two instructions, and a second core can run the same line at the same time. The scheduler decides when, which is why a real race is tested by repetition and not by one run.

lock

A lock admits one thread at a time into its block. The same two threads now add under a private object:

var locked = 0; var gate = new object(); void Add() { lock (gate) { locked = locked + 1; } } var third = new Thread(Add); var fourth = new Thread(Add); third.Start(); fourth.Start(); third.Join(); fourth.Join(); Console.WriteLine($"locked {locked}");

dotnet run

locked 2

The second thread waited at the lock until the first left it, so the read, the addition and the write of one thread happened together. The object is a private field or a local of the class that owns the state, never this, a Type object or a string: another piece of code can lock the same public object and deadlock against this one. A lock is reentrant, so a method that holds it may call another method that locks the same object.

Interlocked

For one counter, a lock is more than the work needs. Interlocked.Increment is a single atomic instruction: no thread can see the value between the read and the write. Four threads add one hundred thousand times each:

var total = 0; var threads = new Thread[4]; for (var i = 0; i < threads.Length; i++) { threads[i] = new Thread(() => { for (var n = 0; n < 100000; n++) { Interlocked.Increment(ref total); } }); threads[i].Start(); } foreach (var thread in threads) { thread.Join(); } Console.WriteLine($"total {total}"); Console.WriteLine($"add {Interlocked.Add(ref total, 100)}");

dotnet run

total 400000
add 400100

Four hundred thousand increments and the exact result, every run, because each one is atomic. Interlocked.Add returns the new value, which is 400 100 after the extra hundred. Interlocked works on a field or a local that is passed by reference, and its methods exist for int, long and references; a double has none, and a counter of that type needs a lock.

CompareExchange and Exchange

The other two operations replace a value and return the old one. CompareExchange writes only when the current value is the one expected, which is the primitive behind a lock-free update of a reference:

var max = 0; Interlocked.CompareExchange(ref max, 10, 0); Console.WriteLine($"max {max}"); Interlocked.CompareExchange(ref max, 20, 0); Console.WriteLine($"max {max}"); var old = Interlocked.Exchange(ref max, 5); Console.WriteLine($"old {old} now {max}");

dotnet run

max 10
max 10
old 10 now 5

The first call found 0 and wrote 10; the second expected 0, found 10 and wrote nothing. Exchange wrote 5 without a condition and returned the old 10. A compare-and-swap loop retries until the comparison succeeds, which is how a concurrent collection updates a slot without a lock; the loop belongs in a library, because a program that writes it by hand usually gets the memory ordering wrong. The next part starts with metadata and reflection.

Exercises

  1. Remove the events from the first program and run it twenty times; how many different results do you get?
  2. Replace Interlocked.Increment with total = total + 1 and run the four threads; read the total.
  3. Use one lock for two counters and confirm that both are exact.
  4. Write a compare-and-swap loop that sets a value only when it is lower than the current one.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What makes a lost update happen?

    Answer

    Two threads read the same value before either writes

    The read, the addition and the write are three steps, and two threads can interleave them.

  2. What does lock do?

    Answer

    Admits one thread at a time into the block

    The second thread waits at the lock until the first leaves it.

  3. What does this program print? var counter = 0; var read = new ManualResetEventSlim(); var written = new ManualResetEventSlim(); var first = new Thread(() => { var copy = counter; read.Set(); written.Wait(); counter = copy + 1; }); var second = new Thread(() => { read.Wait(); var copy = counter; written.Set(); counter = copy + 1; }); first.Start(); second.Start(); first.Join(); second.Join(); Console.WriteLine(counter);

    Answer

    1

    Both threads copied zero, so both wrote one; that is the lost update.

  4. What does Interlocked.Increment return?

    Answer

    The new value of the counter

    It returns the incremented value, which is why Interlocked.Add printed 400100.

  5. What does this program print? var x = 0; Interlocked.Increment(ref x); Console.WriteLine(x);

    Answer

    1

    The atomic increment changed the variable to one.

  6. Which object should a lock use?

    Answer

    A private reference object of the type that owns the state

    A public or shared object can be locked by other code and deadlock with this one.

  7. What does this program print? var x = 0; Console.WriteLine(Interlocked.CompareExchange(ref x, 2, 0));

    Answer

    0

    CompareExchange returns the value it found, which was zero, and then wrote two.

  8. What does Interlocked.Exchange return?

    Answer

    The value that was there before the write

    The measured call returned the old 10 after writing 5.

  9. What does this program print? var x = 5; Console.WriteLine(Interlocked.Exchange(ref x, 9));

    Answer

    5

    The call returns the old value and leaves 9 in the variable.

  10. Can a thread enter a lock it already holds?

    Answer

    Yes, lock is reentrant for the same thread

    The runtime counts the entries of the owning thread, so a nested lock is allowed.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image