Concurrency Comparisons

Side-by-side trade-offs where neither column wins. The workload, the runtime and how much correctness risk you can carry decide — and each comparison ends with the verdict that follows from that, not from a preference.

Lock-based vs lock-free

Lock-free is a progress guarantee — at least one thread always advances — and not a performance claim. Presented as an optimization it is one of the most expensive mistakes available to a team.

DimensionLock-basedLock-free
GuaranteeMutual exclusion; progress depends on the holder finishingSome thread always makes progress, whatever the others do
If a task is descheduled insideEveryone waiting is stuck until it is scheduled againOthers continue; that is the entire point
Uncontended costAn atomic operation plus a little bookkeepingAn atomic operation
Contended costBlock, context switch, wake — expensive but boundedCAS retries burning CPU and cache-line traffic
Multi-word invariantsStraightforward — lock the regionVery hard; usually needs a redesign of the data structure
Memory reclamationNot a problem — the lock says when nobody is lookingA hard open problem: hazard pointers, epochs, RCU
Failure modesDeadlock, convoy, starvation, priority inversionLivelock, ABA, torn invariants, memory-ordering bugs
ReviewabilityMost engineers can review it correctlyVery few can, and the bugs are the subtle kind
Use Lock-based when
  • Essentially always, as the starting point.
  • The invariant spans more than one word.
  • The team must be able to review and maintain the result.
Use Lock-free when
  • A profile shows the lock is the bottleneck and the region cannot shrink.
  • Progress must survive a thread being descheduled — real-time or signal contexts.
  • You are using a well-tested library structure rather than writing your own.
Verdict

Use a lock. If contention is genuinely the bottleneck, first shrink the critical section, then shard the lock, then remove the sharing. Hand-written lock-free code is the last resort, and it should arrive with a benchmark and a memory-reclamation story.