threaded_reader/
threaded_reader.rs

1extern crate progress_streams;
2
3use progress_streams::ProgressReader;
4use std::fs::File;
5use std::io::Read;
6use std::sync::Arc;
7use std::sync::atomic::{AtomicUsize, Ordering};
8use std::thread;
9use std::time::Duration;
10
11fn main() {
12    let total = Arc::new(AtomicUsize::new(0));
13    let mut file = File::open("/dev/urandom").unwrap();
14    let mut reader = ProgressReader::new(&mut file, |progress: usize| {
15        total.fetch_add(progress, Ordering::SeqCst);
16    });
17
18    {
19        let total = total.clone();
20        thread::spawn(move || {
21            loop {
22                println!("Read {} KiB", total.load(Ordering::SeqCst) / 1024);
23                thread::sleep(Duration::from_millis(16));
24            }
25        });
26    }
27
28    let mut buffer = [0u8; 8192];
29    while total.load(Ordering::SeqCst) < 100 * 1024 * 1024 {
30        reader.read(&mut buffer).unwrap();
31    }
32}