Skip to content

Single Producer Multiple Consumer Lock-free Bound Owning queue written in Rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

ShayDamir/spmc_queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spmc_queue

CI codecov Built with cargo-make License: MIT OR Apache-2.0

Single Producer Multiple Consumer Lock-free Bound queue written in Rust.

Can be used to efficiently implement work-stealing algorithms with polling.

The queue is bound and non-blocking. It can contain fixed number of elements, and the push() operation will fail if the queue is full, and pop() will fail if the queue is empty.

Waiting can be implemented by external methods, or a polling method can be used by checking for is_full() or is_empty().

Forward guarantees

push() operation is wait-free, i.e. it will be complete in a fixed amount of time. pop() operation is lock-free, i.e. out of multiple concurrent pop() operations, at least one will succeed. Others will retry the operations until they succeed.

Example

# use std::thread;
use spmc_queue::SpmcQueue;
let mut queue = SpmcQueue::new();
let mut handles = Vec::new();

for t in 0..10 {
     let c = queue.to_consumer();
     handles.push(thread::spawn(move || {
         loop {
            match c.pop() {
             Some(element) => { println!("Consumer {} got {}", t, element); break; }
             None => { thread::yield_now(); }
            }
         }
     }));
}

for i in 0..10 {
    queue.push(i).unwrap();
}
for h in handles {
    h.join().unwrap();
}

About

Single Producer Multiple Consumer Lock-free Bound Owning queue written in Rust

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages