Lock Free Programming

29 Jul 2022

[ c++  concurrency  ]

Testing the lock-free algorithms

  1. Define the requirements of the algorithm in a formal way
  2. Devise theoretical algorithm (pseudo code)
  3. Proof of correctness (by hand or automated theorem prover, e.g. TLA+)
  4. Implement algorithm
  5. Code review by experts
  6. Unit tests to check single-threaded correctness (necessary condition)
  7. Multi-threaded stress tests (check invariants, no data structure corruption)
  8. Code instrumentation for specific multi-threaded scenarios (open problem …)
  9. Verification tools such as CppMem to check the data races etc. (for small code snippets)

Our New Friend Compare Exchange

Key observations

  1. CAS loops are the basis for many lock-free algorithms
  2. CAS can only be used on small data (64 bit usually)
  3. CAS can be used to transfer memory ownership atomically between threads
  4. Lock-free algorithms usually require managing memory and object lifetime carefully
  5. We can add information (e.g. counters) to atomically check for cuncurrent modification with CAS

Disadvantages

Implementation

Notes

Reference