Skip to content

Instantly share code, notes, and snippets.

@pftbest
Last active November 9, 2017 13:51
Show Gist options
  • Save pftbest/3642ffba88e7117094bb2053d27637a1 to your computer and use it in GitHub Desktop.
Save pftbest/3642ffba88e7117094bb2053d27637a1 to your computer and use it in GitHub Desktop.
extern crate heapless;
use std::thread;
use heapless::ring_buffer::RingBuffer;
fn main() {
static mut V: RingBuffer<u8, [u8; 512]> = RingBuffer::new();
let (mut tx, mut rx) = unsafe { V.split() };
const N: u32 = 300;
let t1 = thread::spawn(move || {
let mut sum: u32 = 0;
for i in 0..N {
sum += (i as u8) as u32;
while let Err(_) = tx.enqueue(i as u8) {}
}
println!("pushed: {}", sum);
});
let t2 = thread::spawn(move || {
let mut sum: u32 = 0;
for _ in 0..N {
let mut data = rx.dequeue();
while data.is_none() {
data = rx.dequeue();
}
sum += data.unwrap() as u32;
}
assert!(rx.dequeue().is_none());
println!("popped: {}", sum);
});
println!("Hello, world!");
t2.join().unwrap();
t1.join().unwrap();
println!("All done!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment