Bounded Queue Block/Wake Trace
Prerequisites: Condition Variables and Producer-Consumer Queues, Mutexes, Lock Scope, and Contention
The feed thread puts AAPL updates into a queue, and the strategy thread takes them out. The article's queue can grow without limit; this one is bounded. Trace every put, take, block, and wakeup that a scheduler script causes.
- Bounded blocking queue: holds at most
Cvalues and makes both sides wait. A consumer that finds it empty sleeps until a value arrives; a producer that finds it full sleeps until a slot frees up. - It uses one mutex and two condition variables, one per reason to wait:
- producers wait on
not_full, and every completed take notifies it; - consumers wait on
not_empty, and every completed put notifies it.
- producers wait on
void put(int v) {
std::unique_lock<std::mutex> lock(m);
while (q.size() == capacity) not_full.wait(lock); // the predicate loop
q.push_back(v);
lock.unlock();
not_empty.notify_one(); // a value arrived
}
int take() {
std::unique_lock<std::mutex> lock(m);
while (q.empty()) not_empty.wait(lock);
int v = q.front();
q.pop_front();
lock.unlock();
not_full.notify_one(); // a slot freed up
return v;
}
With real threads, the call order changes from run to run. This problem removes the threads: it is a deterministic, single-threaded simulation of the queue above, driven by a scheduler script whose commands name the thread that runs next and what it does.
What to implement
process_commands(capacity, mode, commands)returns the printed lines, one per event, in the order the events happen.- On stdin: the capacity
C, the mode (notify_oneornotify_all), the number of commandsQ, then theQcommands, one per line. - There are three parts. One solution must pass all ten tests.
| Command | Meaning |
|---|---|
T PUT v | Thread T tries to put the integer v |
T TAKE | Thread T tries to take the oldest value |
STATE | Print the queue and both wait queues |
SPURIOUS T | Thread T wakes up with no notify (Part 3) |
| Printed line | When |
|---|---|
PUT T v | Thread T appends v to the queue |
TAKE T v | Thread T removes v from the front of the queue |
BLOCK T | Thread T joins the back of a wait queue |
WAKE T | Thread T leaves a wait queue |
ERROR T BLOCKED | A PUT or TAKE names a blocked thread |
ERROR T NOT_BLOCKED | A SPURIOUS names a running thread (Part 3) |
STATE output
STATE queue=[...] not_full=[...] not_empty=[...]
- Each list is oldest first, with commas and no spaces. An empty list is
[]. queuelists values andnot_emptylists thread names.not_fulllists each waiting producer asname:valuewith its pending value. In the example below, while P2 is blocked on its put of 12, the list isnot_full=[P2:12].
The model
- Each thread is running or blocked. Every thread starts out running, so a name the script has not used before belongs to a running thread.
- A blocked thread sits in the wait queue of one condition variable: the threads waiting on it, oldest first.
- The C++ standard promises no order for waking waiters. The simulation uses first-in, first-out order, so every script has exactly one correct output.
- A blocked producer keeps its pending value. That value is not in the queue until the put completes.
- A
PUTorTAKEfrom a blocked thread printsERROR T BLOCKEDand changes nothing. A blocked thread is asleep insidewait()and cannot run code, so the line is a script mistake, not an operation.
Every PUT and TAKE is an attempt, one pass through the body of the predicate loop:
| Attempt | Predicate | If it holds | If it fails |
|---|---|---|---|
| put | the queue holds fewer than C values | append the value at the back, print PUT T v, notify not_empty | print BLOCK T, join the back of not_full |
| take | the queue is not empty | remove the front value, print TAKE T v, notify not_full | print BLOCK T, join the back of not_empty |
A notify on an empty wait queue does nothing, as a notification with no waiter evaporates in the article. Otherwise it removes waiters from the front and prints WAKE T for each: notify_one removes the oldest (Part 1), notify_all all of them (Part 2).
A woken thread repeats its attempt with its pending operation: the re-check of the while condition after wait() returns.
- Woken threads re-check one at a time, in wake order, all before the next command runs.
- An operation completed by a woken thread notifies exactly like one completed by a command.
- A real woken thread would first reacquire the mutex, and another thread could run in the meantime. The simulation simplifies this: woken threads re-check before any other thread runs.
Part 1: notify_one
- The mode is
notify_one, so each notify wakes only the oldest waiter. - Nothing runs between that notify and the re-check, so in this part the re-check always succeeds.
- A woken producer appends its pending value behind the values already queued. A woken consumer takes the value at the front.
- A thread that blocks again later joins the back of the wait queue, behind every thread already waiting.
Limits
CandQare between 1 and 105. The output has at most 2 · 105 lines.- Values are integers between -109 and 109, written without leading zeros or a plus sign.
- Thread names have 1 to 8 letters and digits and are never
STATEorSPURIOUS. Names such asP1andC1are only a convention: any thread may put and take.
Tests 1–4 cover this part. They all use notify_one.
Part 2: notify_all
Add the notify_all mode:
- A notify removes every thread from the wait queue, oldest first, and prints a
WAKEline for each before any of them re-checks. - The woken threads re-check in that order, each seeing the queue as the previous one left it.
- The first takes the freed slot or the new value.
- The others find their predicate false, print
BLOCK, and rejoin the back of the wait queue, keeping their order relative to each other.
This is the article's case where another thread takes the item first, and it is why the wait sits inside a while loop rather than an if.
Tests 5–7 cover this part. They all use notify_all.
Part 3: Spurious wakeups
Add SPURIOUS T: thread T returns from wait() with no notify, which the standard permits.
- If
Tis running, including a name the script has not used before, printERROR T NOT_BLOCKEDand change nothing. - Otherwise
Tleaves its wait queue from wherever it stands, printsWAKE T, and re-checks like any woken thread. - In this simulation that re-check always fails, so
TprintsBLOCK Tand rejoins the back of the same wait queue, losing its place.
The re-check fails because a producer waits only while the queue is full and a consumer only while it is empty: every notify hands the freed slot or new value to a woken thread before the next command runs.
- Under
notify_one, the lost place changes which thread a later notify wakes. - Under
notify_all, it changes the order of the re-checks, and so which thread gets the slot or value. - A thread that tested its predicate with
ifinstead ofwhilewould go on to put into a full queue or take from an empty one.
Tests 8–10 cover this part. Tests 8 and 9 use notify_one, and test 10 uses notify_all.
2 notify_one 10 P1 PUT 10 P1 PUT 11 P2 PUT 12 P2 PUT 13 C1 TAKE STATE C1 TAKE C1 TAKE C2 TAKE P1 PUT 14
PUT P1 10 PUT P1 11 BLOCK P2 ERROR P2 BLOCKED TAKE C1 10 WAKE P2 PUT P2 12 STATE queue=[11,12] not_full=[] not_empty=[] TAKE C1 11 TAKE C1 12 BLOCK C2 PUT P1 14 WAKE C2 TAKE C2 14
The queue holds two values. P1 fills it with 10 and 11, so P2's put of 12 blocks. P2 joins not_full holding 12, and its next command is a script mistake that prints ERROR P2 BLOCKED.
C1's take removes 10 and notifies not_full. P2 wakes, re-checks, finds a free slot, and appends 12 behind 11, which STATE confirms.
C1 then takes 11 and 12, and each notify finds no waiter. C2's take finds the queue empty and blocks. When P1 puts 14, the notify on not_empty wakes C2, which re-checks and takes 14 before the next command.