threaded_writer/
threaded_writer.rs

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