Skip to content
You Start with one site
Amounts in Start with one site
Embedded Systems · Concept

Mutex

One idea Advanced 5 min read
Found this useful?

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.

No: 5 + 3 is 8True, if each addition is one move that nothing can interrupt.It is three moves
Yes, if the timing is badOne task writes an answer it worked out from an old value.A lost update
How often?Rarely, and never when you watch for it.This is why it is dangerous

See it

Predict first

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?

See it: two tasks and one total

Total in memory
Total after both tasks should be
Who holds the key

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

The question

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
  1. Add the time the key is held

    4 ms + 30 ms = 34 ms.

  2. Compare with what Task B needs

    Task B must read every 20 ms, so a wait of 34 ms makes it miss a reading.

  3. 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

A mutex does not guard the data. It works only if every task agrees to use it. Nothing stops a task from writing the total without taking the key first. One task that does this is enough to bring the lost update back. Find every place that touches the shared thing and put the same take and give around each one.

Check yourself

Question 1

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?

Question 2

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?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.