Lock-Free and Wait-Free Alternatives
Lock-Free vs Wait-Free
Lock-free: At least one thread always makes progress. No thread can block another forever. Deadlock impossible.
Wait-free: Every thread makes progress in bounded steps. Even stronger guarantee. Very hard to implement.
How It Works
Use Compare-And-Swap (CAS) operations. Read value, compute new value, atomically update if unchanged. If changed by another thread, retry. No locking needed.
Trade-offs
Pros: No deadlock possible. Often faster under low contention. No priority inversion.
Cons: Much harder to implement correctly. Subtle bugs. Under high contention, CAS retry loops may burn CPU. Limited to simple data structures.
When to Use
Use lock-free data structures from libraries (ConcurrentHashMap, AtomicInteger). Writing your own is expert-level. For most applications, well-designed locking is simpler and sufficient.