1/15
Looks like no tags are added yet.
What is a thread?
A lightweight execution unit inside a process: one independent path through its code
What is private to each thread?
Its ID, state, context (registers and program counter) and stack
What do all threads of a process share?
The code, global data and heap
Why is a thread switch faster than a process switch?
It does not switch the address space, though registers are still saved and loaded
What are the five thread states?
New, runnable, waiting or blocked, timed waiting, terminated
What is preemptive threading?
The runtime may hand control from one thread to another at any time, and higher priority gets precedence
What is non-preemptive threading?
Once a thread starts, it cannot be stopped or give up control until it finishes its task
What is a race condition?
The result depends on the timing and interleaving of threads, and some interleavings give a wrong answer
What is a data race?
Two or more threads access the same variable concurrently and at least one access is a write
Why is x = x + 1 not atomic?
It is three steps (load, add, store), and another thread can step in between them
Why can a race happen even on a single core?
A preemptive context switch can interleave two threads between any two instructions, so no parallelism is needed
What is a critical section?
Code where a data race may occur, such as an update to shared data
What are the three standard fixes for a critical section?
Atomize it, disable preemption inside it, or sequentialize access with locks or mutexes
Why is disabling preemption not enough?
It stops interleaving on one core but not threads running in parallel on other cores
Why does the sum example lock only the final update?
The partial sums are private and computed in parallel, so only the update of the shared total needs mutual exclusion
What is non-deterministic execution?
The order of operations is not fully fixed by the program, as in parallel programs