Chapter 9

Concurrency, scheduling, publication, and time

This chapter explains what can go wrong when several activities share data or a processor, and which tools make that sharing safe. It begins with the difference between concurrency §9.1 and parallelism, then defines race conditions §9.2 and data races §9.3, critical sections §9.4, and the mechanisms that protect them: mutexes §9.5, semaphores §9.6, spinlocks §9.7, atomic operations §9.8 and compare-and-swap §9.9. Next comes the memory model that says which writes a thread can see, with the happens-before relation §9.10, acquire-release ordering §9.11 and sequential consistency §9.12, followed by the progress guarantees called lock-free §9.13 and wait-free.

The middle of the chapter covers the failures of locking and scheduling: deadlock §9.14 and lock ordering §9.15, priority inversion §9.16 and priority inheritance §9.17, starvation §9.18 and head-of-line blocking §9.19. It then shows how readers can keep using shared data while a writer replaces it: read-copy update §9.20 and its grace periods §9.21, reference counting §9.22, coherent snapshots §9.23 and immutable publication §9.24, together with the ABA problem §9.25, in which a value changes and changes back unnoticed, and the time-of-check to time-of-use §9.26 error. The last part is about time and flow: reentrant code, asynchronous completion §9.28 and cancellation §9.29, backpressure §9.30 and bounded queues §9.31, response time versus execution time §9.32, worst-case execution time §9.33, deadlines and timeouts §9.34, monotonic and wall clocks §9.36, schedulability §9.37, and rate limiting §9.38. The entries on the volatile qualifier §4.34 and on memory barriers §4.37 in the memory chapter are useful background.

In this chapter

  1. 9.1 Concurrency versus parallelism
  2. 9.2 Race condition
  3. 9.3 Data race
  4. 9.4 Critical section
  5. 9.5 Mutex
  6. 9.6 Semaphore
  7. 9.7 Spinlock
  8. 9.8 Atomic operation
  9. 9.9 Compare-and-swap
  10. 9.10 Happens-before relation
  11. 9.11 Acquire-release ordering
  12. 9.12 Sequential consistency
  13. 9.13 Lock-free versus wait-free
  14. 9.14 Deadlock
  15. 9.15 Lock ordering
  16. 9.16 Priority inversion
  17. 9.17 Priority inheritance
  18. 9.18 Starvation
  19. 9.19 Head-of-line blocking
  20. 9.20 Read-copy update (RCU)
  21. 9.21 Grace period
  22. 9.22 Reference counting
  23. 9.23 Coherent snapshot
  24. 9.24 Immutable publication
  25. 9.25 ABA problem
  26. 9.26 Time-of-check to time-of-use (TOCTOU)
  27. 9.27 Reentrancy
  28. 9.28 Asynchronous completion
  29. 9.29 Cancellation
  30. 9.30 Backpressure
  31. 9.31 Bounded queue
  32. 9.32 Response time versus execution time
  33. 9.33 Worst-case execution time (WCET)
  34. 9.34 Deadline versus timeout
  35. 9.35 Monotonic clock
  36. 9.36 Wall clock and clock synchronization
  37. 9.37 Schedulability
  38. 9.38 Rate limiting