Master the fundamental concepts of lock-free & wait-free programming through this focused micro-challenge.
A lock-free stack pushes and pops via compare_exchange_weak on the head pointer. Michael and Scott's queue extends the idea to two ends. Windows' InterlockedPushEntrySList, Java's ConcurrentLinkedQueue, and Rust crossbeam all trace back to these CAS loops.
Read head, link new node to head, CAS head from observed value to new node. On failure, retry with updated head.
cLoading…
compare_exchange_weak may spuriously fail; loops must retryKeep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will implement a lock-free stack with CAS push and pop. This exercise requires correct retry loops and a concurrent test with multiple threads.
Implement lock-free stack using atomic compare-and-swap.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github