Sharing safely
When several threads use the same data at the same time, things go wrong in ways that are hard to see. See Race Conditions. The tools for preventing that are called synchronization primitives.
There are a handful, and their names are intimidating. One question sorts most of them:
How many threads may pass at once?
Waiting in line
Inside
Done
Start. One thread inside at a time. Six threads arrive wanting the same shared data. Each needs 2 ticks with it.
- Ticks
- 0
- Inside now
- 0
- Most inside at once
- 0
Mutex: one at a time
A mutex (short for mutual exclusion) is the simplest. It is a lock with one key.
A thread locks the mutex before touching the shared data
If it is free, the thread goes straight in.
Any other thread that tries to lock it must wait
It is paused until the mutex is free.
The first thread unlocks when it has finished
One waiting thread is let in.
Waiting in line
Inside
Done
Start. One thread inside at a time. Six threads arrive wanting the same shared data. Each needs 2 ticks with it.
- Ticks
- 0
- Inside now
- 0
- Most inside at once
- 0
Six threads, strictly one after another. Safe, and slow if the protected section is long.
import threading
lock = threading.Lock()
def add_to_total(amount):
global total
with lock: # locks here, unlocks at the end of the block
total = total + amountUse a mutex when a piece of data must never be touched by two threads at once. It is the default choice.
Semaphore: N at a time
A semaphore is a counter of permits. It starts at some number N.
- To enter, a thread takes a permit. If none are left, it waits.
- When it leaves, it hands the permit back.
So at most N threads are inside together.
Waiting in line
Inside
Done
Start. Up to three threads inside at a time. Six threads arrive wanting the same shared data. Each needs 2 ticks with it.
- Ticks
- 0
- Inside now
- 0
- Most inside at once
- 0
A semaphore does not make shared data safe: three threads are in there at once. Its job is different. It limits how many do something at the same time.
Example
What semaphores are for
A database allows 10 connections. A semaphore with 10 permits stops an eleventh thread from trying.
You need to download 500 files but only want 5 downloads in flight. A semaphore with 5 permits.
A semaphore with one permit behaves much like a mutex.
Read-write lock: many readers or one writer
Reading shared data does not change it, so any number of threads can read together without harm. Problems only arise when someone writes. A read-write lock uses that.
- Any number of readers may hold it together.
- A writer must have it alone: no readers, no other writers.
Waiting in line
Inside
Done
Start. Any number of readers together, or one writer alone. Six threads arrive wanting the same shared data. Each needs 2 ticks with it.
- Ticks
- 0
- Inside now
- 0
- Most inside at once
- 0
Readers went in side by side. Each writer went in alone. The total time is well under the mutex's, with the same safety.
Use it for data that is read far more often than it changes, such as settings or a cache. If writes are frequent, a plain mutex is simpler and often just as fast.
The other three
These do not fit the "how many at once" picture as neatly, but you will meet them.
Atomic operation: one indivisible step
Some small operations, such as adding one to a number or swapping a value, can be done by the processor as a single step that cannot be interrupted. No lock, no waiting.
AtomicInteger counter = new AtomicInteger();
counter.incrementAndGet(); // safe from any number of threadsIt is the fastest option, and only works for simple single-value updates.
Condition variable: wait until something is true
Sometimes a thread has nothing to do until another thread changes something: "wait until the queue is not empty". A condition variable lets a thread sleep, without using any CPU, until another thread signals that things have changed. It is always used together with a mutex.
Channel: hand the data over
All of the above let threads share memory carefully. A channel avoids sharing altogether. It is a pipe: one thread puts a value in, another takes it out. Only one thread has the value at any time, so there is nothing to lock.
jobs := make(chan Job)
go func() { jobs <- job }() // one side sends
next := <-jobs // the other receivesThe saying that goes with it: do not communicate by sharing memory. Share memory by communicating.
All six, sorted
| Primitive | How many pass at once | Use it for |
|---|---|---|
| Mutex | 1 | Protecting shared data |
| Read-write lock | Many readers, or 1 writer | Data that is mostly read |
| Semaphore | Up to N | Limiting use of a scarce resource |
| Atomic operation | Everyone, each in one indivisible step | Counters and flags |
| Condition variable | Nobody, until signalled | Waiting for a change |
| Channel | 1 holds each item | Passing work between threads |
Which one?
Can you avoid sharing?
Give each thread its own data, or pass data through a channel. No sharing means nothing to protect.
Is it a single counter or flag?
Use an atomic operation.
Otherwise, start with a mutex
Keep the locked section as short as possible.
Reach for the others when you have their specific need
Mostly reads: read-write lock. A limit on simultaneous use: semaphore. Waiting for a condition: condition variable.
Key takeaways
- Synchronization primitives control how threads access shared things.
- A mutex lets one thread in at a time. It is the default for protecting shared data.
- A semaphore lets up to N in. It limits simultaneous use and does not by itself protect data.
- A read-write lock lets many readers in together, or one writer alone.
- Atomic operations, condition variables and channels cover single-value updates, waiting, and passing data without sharing.