Thread models and lifecycle ownership
See what one blocking call does to the other threads under each kernel mapping, and who owns a thread's argument and result from create to join. Threads and concurrency contracts · Operating systems.
A web server runs three threads in one process on a machine with 2 cores. Ada needs 1 tick of computing, then calls read() on a slow disk, which takes 3 ticks, then needs 1 more tick. Ben and Cy need 1 tick each and never block on I/O. While Ada waits for the disk, do Ben and Cy keep running? And once a thread has finished, who gets the result it leaves behind? Both answers come from how threads map onto what the kernel schedules and from the join-or-detach choice your code makes.
What a thread owns
A thread is one flow of execution inside a process. It has its own program counter, registers and stack, so each has its own execution state. Threads of the process share the address space, including the heap and global variables, as well as open files. So Ben can read a buffer that Cy allocated on the heap, but a local variable of Cy may live in Cy's stack frame, and that frame ends when Cy's function returns. Each stack is separate, but nothing stops another thread reading it through a pointer. Keep that in mind; it matters again when threads are created.
Which thread does the kernel see?
The kernel schedules kernel entities, often called kernel threads. A program's own threads are user threads, and the mapping decides how many kernel entities stand behind them.
- Many-to-one: every user thread of the process shares one kernel entity. The kernel sees one thing to run, so only one thread runs at a time, on one core. If any thread makes a blocking system call, the entity blocks and every other thread waits with it.
- One-to-one: each user thread has its own kernel entity. Threads can run on different cores, and a blocking call stops only the thread that made it. The price is one kernel entity for every thread.
- Many-to-many: a runtime spreads many user threads over a smaller number of kernel entities. A blocking call blocks the entity executing it. Other entities can still run ready user threads; whether a runtime moves waiting user threads onto those entities depends on its scheduler. The price is a runtime that must schedule user threads on top of the kernel.
One blocking call under two mappings
One blocking read under two mappings
Read the steps as text
- Ada, Ben and Cy share one process on 2 cores. Many-to-one puts every thread on one kernel entity. One-to-one gives each thread its own kernel entity.
- Many-to-one: Ada runs on core 1 and calls disk read; Ben and Cy wait for the one kernel entity. One-to-one: Ada runs on core 1 and calls disk read; Ben runs on core 2 and finishes; Cy waits for a free core.
- Many-to-one: Ada stays blocked in disk read, 2 ticks to go; Ben and Cy are stalled while Ada is blocked. One-to-one: Ada stays blocked in disk read, 2 ticks to go; Cy runs on core 1 and finishes.
- Many-to-one: Ada stays blocked in disk read, 1 tick to go; Ben and Cy are stalled while Ada is blocked. One-to-one: Ada stays blocked in disk read, 1 tick to go.
- Many-to-one: Ada returns from disk read and is ready; Ben and Cy were stalled until Ada's disk read ended. One-to-one: Ada returns from disk read and is ready.
- Many-to-one: Ada runs on core 1 and finishes; Ben and Cy wait for the one kernel entity. One-to-one: Ada runs on core 1 and finishes.
- Many-to-one: Ben runs on core 1 and finishes; Cy waits for the one kernel entity. One-to-one: nothing is left to run.
- Many-to-one: Cy runs on core 1 and finishes. One-to-one: nothing is left to run.
Run the server with both mappings. Under many-to-one, Ada runs in tick 1 and calls read(). The entity then blocks in ticks 2 to 4. Ben and Cy are ready and two cores sit idle, yet nothing can run them: that is 3 ticks for each of 2 threads, 6 stalled ticks. Ada finishes in tick 5, Ben in tick 6 and Cy in tick 7.
Under one-to-one, Ada and Ben start on the two cores in tick 1, and Ben finishes at once. Ada's call frees her core, so Cy runs on it and finishes in tick 2. Ada finishes in tick 5 under both mappings, so the call cost her the same. Under many-to-one her siblings paid for it, and the last thread finished in tick 7 instead of tick 5.
Create, finish, collect
A thread starts when another thread creates it, naming a start routine and one argument. The new thread may start before or after its creator's next line, so the creator cannot assume its own state is still there when the worker reads the argument.
A creator returns before the worker reads
Read the steps as text
- Maker creates Ada, one action per tick. Ada gets an address into Maker's frame. Ada is joinable, but Maker never joins Ada.
- Maker creates Ada (joinable) with the address of a local in Maker's frame.
- Maker returns, and the frame is gone; Ada has not started.
- Ada starts, reads the address in Maker's dead frame (dangling) and finishes, and the result waits for a join.
Here Maker, a helper thread rather than the program's main thread, creates Ada and passes the address of a local variable in Maker's own frame, then returns in tick 2. Ada is not scheduled until tick 3. She attempts to read through the address then, but Maker's frame is gone, so the address is dangling and dereferencing it is undefined behavior.
There are three fixes: pass a copy of the value, pass a heap object that outlives the creator, or make the creator wait until the worker has read it. Waiting cures only the dangling read. If the address points at a variable the creator keeps changing, such as a loop counter, a late-starting worker can observe a later value even when accesses are synchronized. Give each worker its own copy to preserve the intended value; unsynchronized concurrent reads and writes can also cause a data race.
POSIX threads are joinable by default; you can also detach a joinable thread after creation, even after it terminates, to release its retained resources. A detached thread cannot be joined, and its thread resources are released on termination. It can still communicate a result through other communication, such as synchronized shared memory. In the animation above, Ada remains joinable and unjoined when the run ends; the process has not yet terminated.
Wait in a join, collect a result, release a detached worker
Read the steps as text
- Maker creates Ben and Ada, one action per tick. Ben gets a copy of the value. Ada gets an address into Maker's frame. Maker joins Ada, so Ada's result is collected. Ben is detached and cannot be joined.
- Maker creates Ben (detached) with a copy of a value.
- Maker creates Ada (joinable) with the address of a local in Maker's frame; Ben starts, reads a copy of the value and finishes, and the record is released.
- Maker waits for Ada to finish; Ada starts and reads the address in Maker's live frame, 1 tick to go.
- Maker waits for Ada to finish; Ada finishes, and the result waits for a join.
- Maker joins Ada and takes the result.
- Maker returns, and the frame is gone.
In this run Maker creates Ben, detached with a copy of a value, and then Ada, joinable with the address of a local, and then joins Ada. Ben finishes in tick 2 and his record is released, with no exit status available to join. In this example Maker does not modify the value, and waits in the join, so its frame is still alive when Ada reads it in tick 3. Ada finishes in tick 4 and her result is kept for the join. In tick 5 Maker's join returns with that result and Ada's record disappears. Only then does Maker return, in tick 6.
Reuse workers with a pool
In a one-to-one implementation, creating a thread for every task costs a stack and a kernel entity each time. A thread pool starts a fixed number of worker threads and queues the tasks. Each idle worker takes the next queued task.
Four tasks on two workers and on four
Read the steps as text
- Resize, Upload, Tag and Mail wait in one queue. Each idle worker takes the next queued task. Pool of 2 has 2 workers. Pool of 4 has 4 workers.
- Pool of 2: Worker 1 takes Resize; Worker 2 takes Upload and finishes it. Pool of 4: Worker 1 takes Resize; Worker 2 takes Upload and finishes it; Worker 3 takes Tag; Worker 4 takes Mail and finishes it.
- Pool of 2: Worker 1 finishes Resize; Worker 2 takes Tag. Pool of 4: Worker 1 finishes Resize; Worker 3 runs Tag.
- Pool of 2: Worker 1 takes Mail and finishes it; Worker 2 runs Tag. Pool of 4: Worker 3 finishes Tag.
- Pool of 2: Worker 2 finishes Tag. Pool of 4: nothing is left to run.
Four tasks, Resize, Upload, Tag and Mail, need 2, 1, 3 and 1 ticks. With a pool of 2 workers, never more than 2 tasks run at once and the last one finishes in tick 4. With 4 workers all four start in tick 1 and the last finishes in tick 3. The pool trades one tick of waiting for a hard limit on how many threads exist.
Recap
- A thread owns its registers and stack and shares the process address space, so a creator's local variable is gone when its frame ends.
- Under many-to-one one blocking call stalls every thread and only one runs at a time; under one-to-one only the caller waits and threads can run on separate cores.
- A terminated joinable thread retains its exit status until join or detach, a detached thread's resources are released on termination, and a pool reuses a fixed set of workers for a longer queue.
Where the numbers come from
On Linux, glibc's POSIX threads use the one-to-one model: each POSIX thread has its own kernel scheduling entity. A runtime can still multiplex its own tasks over those kernel threads. A runtime using one kernel thread for all its tasks stalls those tasks if it makes a blocking call on that thread.
In this POSIX-style example, a thread moves from ready to running, may block and become ready again, then terminates when its routine returns. A joinable thread retains its exit status and some resources after termination until a successful join or detach. A join waits if necessary and can collect the exit status.
Every thread acts once per tick, a thread keeps its core until its computing segment ends and nothing preempts it. Real kernels preempt and time-slice, and a real read takes unpredictable time. The animations leave out many-to-many, cancellation and thread-local storage, so treat the ticks as a way to compare designs, not as measurements.
Continue to Thread safety and reentrancy after checking the quiz.