Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StaticArrayQueue, a queue that can be used as a global static variable #1024

Closed
wants to merge 9 commits into from

Conversation

Finomnis
Copy link

@Finomnis Finomnis commented Aug 28, 2023

Motivation

Currently there is no queue (that I could find) that fulfills all the following criteria:

  • no_std compatible (without alloc)
  • lock-free
  • usable as a static variable

crossbeam_queue::ArrayQueue is very close, but has the following problems:

  • Not usable as a static variable (new() function isn't const)
  • Requires alloc

Content

This change introduces StaticArrayQueue, which is identical to ArrayQueue except that it contains the array unboxed as a member and has a const constructor.

This allows the following pattern:

use std::{
    thread::{self, sleep},
    time::Duration,
};

use crossbeam_queue::StaticArrayQueue;

static QUEUE: StaticArrayQueue<u32, 10> = StaticArrayQueue::new();

fn main() {
    thread::spawn(|| loop {
        while let Some(element) = QUEUE.pop() {
            println!("Got element: {}", element);
        }
        sleep(Duration::from_millis(1));
    });

    for i in 0..10 {
        QUEUE.push(i).unwrap();
        sleep(Duration::from_millis(10));
    }
}

Open questions/tasks

  • Const generics are not supported by Rust 1.38. Bump MSRV? Publish as external crate? (in its current state, the code requires Rust 1.61. So most likely publish this as an external crate)

@Finomnis
Copy link
Author

Never mind. It seems like this problem is already solved by heapless::mpmc. Will use that one in my project instead.

@Finomnis Finomnis closed this Sep 10, 2023
@Finomnis Finomnis deleted the static_array_queue branch September 10, 2023 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant