Skip to content
Discussion options

You must be logged in to vote

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),
}
}

/// Adds an item to the back of the queue
pub fn enqueue(&self, t: T) {
    let new_node = Box::into_raw(Box::new(Node {
        value: Some(t),
    …

Replies: 1 comment

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Ask and answer questions about GitHub features and usage Programming Help Discussions around programming languages, open source and software development Welcome 🎉 Used to greet and highlight first-time discussion participants. Welcome to the community! source:ui Discussions created via Community GitHub templates
2 participants