Deadlock and Lock-Free Reasoning Lab
Retrieval Prompts
- State the four Coffman conditions for deadlock.
- State which Coffman condition each of these fixes breaks: global lock ordering;
trylock+ backoff; transactional rollback; bounded resource pool. - Define lock-free progress versus wait-free progress.
- Describe the ABA problem in one paragraph.
- State what
memory_order_acquirepairs with and what that pairing guarantees.
Compare and Distinguish
Separate these pairs:
- deadlock versus livelock versus starvation
- prevention versus detection versus avoidance
memory_order_relaxedversusmemory_order_seq_cst- a Treiber lock-free stack versus a mutex-protected stack (safety, progress, memory reclamation)
- reader-preferring, writer-preferring, and FIFO fair rwlocks
Common Mistake Check
Diagnose each. Name the concurrency failure class:
- Two threads each call
transfer(a, b, 100)andtransfer(b, a, 50)respectively. The program hangs. (What failed? What fix?) - A lock-free queue works under load but leaks memory visibly over hours. (What failed?)
- A rwlock implementation passes correctness tests but on a 16-core server a writer waits 40 seconds to acquire while readers continuously enter. (What failed?)
- A CAS loop on a shared counter gets the correct sum under 2 threads but loses updates under 64 threads. (What could cause this?)
- A
sync.RWMutexprotected cache in Go gives correct results but CPU profiling shows huge time inruntime.semacquire. (What failed?)
Mini Application
Do all three:
- Coffman analysis. For the deadlock-prone dining-philosophers implementation, draw the waits-for graph at the moment of deadlock. Identify the cycle. Apply the resource-ordering fix, redraw the graph, and confirm no cycle is possible.
- Readers-writers fairness trace. For a reader-preferring rwlock, construct a write operation that waits arbitrarily long because new readers keep arriving. Convert to writer-preferring and show the symmetric starvation. Design a FIFO scheme and argue bounded waiting.
- ABA walkthrough. Construct a 4-thread schedule on the Treiber stack that exhibits ABA. Show the stack state at each step. Then describe how a tagged-pointer CAS (or hazard pointers, epoch-based reclamation) blocks the schedule.
Evidence Check
This page is complete only if you can:
- produce each trace or graph without looking at the concept page
- name each Coffman condition a given fix breaks
- produce a valid ABA schedule and a mitigation that blocks it
- state the fairness contract of at least one real rwlock in production use (
std::shared_mutex,sync.RWMutex, JavaReentrantReadWriteLock)