Providing lock-free queue with generation counters
I'm preparing a concise C++ implementation of the Michael-Scott lock-free queue using atomic compare-and-swap with generation counters to avoid the ABA problem, noting this example omits full memory reclamation and is for demonstration.
Below is a minimal C++11-style example of a lock-free (Michael–Scott) queue that uses a tagged pointer (pointer + generation counter) to mitigate ABA. For brevity, it omits advanced memory reclamation techniques such as hazard pointers or epoch-based reclamation; in production code, you should integrate one of those schemes to safely reclaim nodes.
#include <atomic>
#include <cstdint>
#include <memory>
#include <optional>
// A pointer tagged with a generation counter to mitigate ABA.
template <typename T>
struct TaggedPtr {
T* ptr;
std::uint64_t tag; // incremented on each pointer change
bool operator==(const TaggedPtr& other) const {
return ptr == other.ptr && tag == other.tag;
}
};
// A node in the queue, also storing a tagged "next" pointer.
template <typename T>
struct Node {
T value;
std::atomic<TaggedPtr<Node>> next;
explicit Node(const T& val)
: value(val)
{
next.store({nullptr, 0}, std::memory_order_relaxed);
}
};
// Lock-free Michael–Scott queue with tagged pointers
template <typename T>
class LockFreeQueue {
public:
LockFreeQueue() {
// Create a dummy sentinel node
Node<T>* dummy = new Node<T>(T{});
head.store({dummy, 0}, std::memory_order_relaxed);
tail.store({dummy, 0}, std::memory_order_relaxed);
}
~LockFreeQueue() {
// Not safe without proper reclamation; demo only
// This will leak nodes if called before threads have finished
}
void enqueue(const T& val) {
Node<T>* newNode = new Node<T>(val);
TaggedPtr<Node> tailSnapshot;
for (;;) {
tailSnapshot = tail.load(std::memory_order_acquire);
TaggedPtr<Node> nextSnapshot = tailSnapshot.ptr->next.load(std::memory_order_acquire);
// Check if tail is consistent
if (tailSnapshot == tail.load(std::memory_order_acquire)) {
// If tail points to last node (next == nullptr), try to link newNode
if (nextSnapshot.ptr == nullptr) {
TaggedPtr<Node> newNext{ newNode, nextSnapshot.tag + 1 };
if (tailSnapshot.ptr->next.compare_exchange_weak(
nextSnapshot, newNext,
std::memory_order_release, std::memory_order_relaxed)) {
// Enqueue succeeded; now try to swing tail to the new node
TaggedPtr<Node> newTail{ newNode, tailSnapshot.tag + 1 };
tail.compare_exchange_strong(
tailSnapshot, newTail,
std::memory_order_release, std::memory_order_relaxed);
return;
}
} else {
// tail is behind; try to advance it
TaggedPtr<Node> newTail{ nextSnapshot.ptr, tailSnapshot.tag + 1 };
tail.compare_exchange_strong(
tailSnapshot, newTail,
std::memory_order_release, std::memory_order_relaxed);
}
}
}
}
// Returns std::optional<T> for convenience; empty if queue is empty
std::optional<T> dequeue() {
TaggedPtr<Node> headSnapshot;
for (;;) {
headSnapshot = head.load(std::memory_order_acquire);
TaggedPtr<Node> tailSnapshot = tail.load(std::memory_order_acquire);
TaggedPtr<Node> nextSnapshot = headSnapshot.ptr->next.load(std::memory_order_acquire);
if (headSnapshot == head.load(std::memory_order_acquire)) {
if (nextSnapshot.ptr == nullptr) {
// Queue empty
return std::nullopt;
}
if (headSnapshot.ptr == tailSnapshot.ptr) {
// Tail is falling behind, try to advance
TaggedPtr<Node> newTail{ nextSnapshot.ptr, tailSnapshot.tag + 1 };
tail.compare_exchange_strong(
tailSnapshot, newTail,
std::memory_order_release, std::memory_order_relaxed);
} else {
// Read the value before CAS (safe because nextSnapshot.ptr is valid if CAS succeeds)
T value = nextSnapshot.ptr->value;
TaggedPtr<Node> newHead{ nextSnapshot.ptr, headSnapshot.tag + 1 };
if (head.compare_exchange_weak(
headSnapshot, newHead,
std::memory_order_release, std::memory_order_relaxed)) {
// In a real implementation, use hazard pointers or epoch-based reclamation
// to safely retire headSnapshot.ptr. Here we simply leak or delete unsafely:
// delete headSnapshot.ptr; // NOT safe without proper reclamation!
return value;
}
}
}
}
}
private:
// head and tail are atomic tagged pointers
std::atomic<TaggedPtr<Node>> head;
std::atomic<TaggedPtr<Node>> tail;
};
Notes
-
Tagged Pointer for ABA: Each atomic pointer includes a tag that increments on every pointer update. This ensures that even if a node is freed and reallocated at the same address (ABA scenario), the tag mismatch will cause CAS to fail.
-
Memory Reclamation: This code does not implement safe node reclamation. In practice, you need hazard pointers, epoch-based reclamation, or another safe method to prevent use-after-free when threads are still referencing nodes.
-
Correctness: The algorithm is multi-producer, multi-consumer and follows the classic Michael–Scott approach. With a suitable reclamation scheme added, this is a practical lock-free queue for many concurrent scenarios.