Should we focus on memory reclamation (like the ABA problem) or move to a low-level systems challenge? #192739
-
🏷️ Discussion TypeQuestion BodyImplement a Lock-Free Thread-Safe Queue in C++ or Rust using only atomic primitives (compare-and-swap). It must support multiple concurrent producers and consumers without using any mutexes or semaphores. Guidelines
|
Beta Was this translation helpful? Give feedback.
Answered by
AbrarBb
Apr 15, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use std::sync::atomic::{AtomicPtr, Ordering};
use std::ptr;
/// A node in the lock-free queue
struct Node {
value: Option,
next: AtomicPtr<Node>,
}
/// A multi-producer, multi-consumer lock-free queue
pub struct LockFreeQueue {
head: AtomicPtr<Node>,
tail: AtomicPtr<Node>,
}
impl LockFreeQueue {
/// Creates a new queue with a dummy sentinel node
pub fn new() -> Self {
let sentinel = Box::into_raw(Box::new(Node {
value: None,
next: AtomicPtr::new(ptr::null_mut()),
}));
Self {
head: AtomicPtr::new(sentinel),
tail: AtomicPtr::new(sentinel),
}
}