two_threads/
two_threads.rs

1const N: usize = 8;
2
3fn producer(mut p: fring::Producer<N>) {
4    let mut index = 'a' as u8;
5    for _ in 0..8 {
6        std::thread::sleep(std::time::Duration::from_millis(25));
7        let mut w = p.write(3);
8        for i in 0..w.len() {
9            w[i] = index;
10            index += 1;
11        }
12        println!("write \"{}\"", std::str::from_utf8(&*w).unwrap());
13    }
14}
15
16fn consumer(mut c: fring::Consumer<N>) {
17    loop {
18        std::thread::sleep(std::time::Duration::from_millis(60));
19        let r = c.read(usize::MAX);
20        if r.len() == 0 {  // buffer is empty
21            break;
22        }
23        println!("            read \"{}\"", std::str::from_utf8(&*r).unwrap());
24    }
25}
26
27fn main() {
28    let mut b = fring::Buffer::<N>::new();
29    let (p, c) = b.split();
30    std::thread::scope(|s| {
31        s.spawn(|| {
32            producer(p);
33        });
34        consumer(c);
35    });
36}