Mutex
On this page
A fuel store has one key. The person who holds the key is the only one inside, and everybody else waits at the door until the key comes back. Programs with several tasks need the same rule for anything the tasks share. A mutex (mutual exclusion) is a key that only one task can hold at a time: a task takes it before it uses the shared thing and gives it back afterwards.
See it
A total of pumped water stands at 100 L. Task A must add 5 L and Task B must add 3 L. Task A reads the total and works out its answer. Before Task A writes, the scheduler runs Task B, which reads, adds and writes. Then Task A writes. What is the total?
Time runs down the table, one move in each row. The left column is the total that both tasks share. Without the key, Task B can run between the read and the write of Task A. With the key, Task B must wait until Task A has finished.
The idea
A task does not change a shared value in one move. It reads the value, works out the new one and writes it back. The scheduler can stop a task between any two of these moves and run another task. If the second task changes the same value in that gap, one of the two changes is lost.
A mutex closes the gap. Before a task touches the shared thing it takes the mutex. If another task holds it, the scheduler blocks the new task until the holder gives the mutex back. So the read, the sum and the write of one task are complete before the next task begins.
Every task that needs the mutex waits while it is held, so hold it for the shortest time you can.
One limit: two tasks that each hold one mutex and wait for the mutex of the other will wait for ever. This is deadlock, and the lesson on semaphores and mutexes shows the cure.
One worked number
Task A takes the mutex of a shared sensor bus, reads a sensor (4 ms), prints the result on a slow serial port (30 ms) and then gives the mutex. Task B needs the bus for 4 ms in every 20 ms. What is the longest time Task B can wait, and how do you make it shorter?
Show the worked solution, 3 steps
- Add the time the key is held
4 ms + 30 ms = 34 ms.
- Compare with what Task B needs
Task B must read every 20 ms, so a wait of 34 ms makes it miss a reading.
- Hold the key for the shared part only
Printing does not use the bus. Give the mutex after the read, then print. The key is then held for 4 ms, the longest Task B can wait.
The mistake to avoid
Check yourself
An energy counter holds 2,340 Wh. Task A must add 12 Wh and Task B must add 7 Wh. No mutex is used. Task B reads first. Then Task A reads, adds and writes. Then Task B writes its answer. What does the counter hold?
Three tasks update one total. Two of them take the mutex first. The third writes the total directly, because its update is very short. Is the total safe?
A task takes a mutex, copies a shared block of readings (1 ms), calculates on its own copy for 50 ms and then gives the mutex. Other tasks complain that they wait too long. What is the best change?
Where you meet it
- Several sensors on one busEach task reads its own sensor, and a mutex gives the two shared wires to one task at a time.
- The totals of an energy meterOne task adds each new measurement and another task sends the totals. The key stops a total from being sent half updated.
- A data logger with a memory cardThe task that writes records and the task that sends them share one file.
- A change to shared data is three moves: read, work out, write. A task can be stopped between them.
- A mutex is a key that one task holds at a time. Take it before, give it back after.
- Every task that touches the shared thing must use the same mutex.
- Hold the key for the shortest time you can.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.