Crates.io | synchronized-writer |
lib.rs | synchronized-writer |
version | 1.1.11 |
source | src |
created_at | 2018-10-24 11:45:34.781413 |
updated_at | 2022-03-19 05:28:00.788322 |
description | A tiny implement for synchronously writing data. |
homepage | https://magiclen.org/synchronized-writer |
repository | https://github.com/magiclen/synchronized-writer |
max_upload_size | |
id | 92367 |
size | 7,824 |
A tiny implement for synchronously writing data.
use synchronized_writer::SynchronizedWriter;
use std::sync::{Arc, Mutex};
use std::thread;
use std::io::Write;
let data = Mutex::new(Vec::new());
let data_arc = Arc::new(data);
let mut threads = Vec::with_capacity(10);
for _ in 0..10 {
let mut writer = SynchronizedWriter::new(data_arc.clone());
let thread = thread::spawn(move || {
writer.write(b"Hello world!").unwrap();
});
threads.push(thread);
}
for thread in threads {
thread.join().unwrap();
}
assert_eq!(b"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!".to_vec(), *data_arc.lock().unwrap());
use synchronized_writer::SynchronizedOptionWriter;
use std::sync::{Arc, Mutex};
use std::io::Write;
let data = Mutex::new(Some(Vec::new()));
let data_arc = Arc::new(data);
let mut writer = SynchronizedOptionWriter::new(data_arc.clone());
writer.write(b"Hello world!").unwrap();
writer.flush().unwrap();
let data = data_arc.lock().unwrap().take().unwrap(); // remove out the vec from arc
assert_eq!(b"Hello world!".to_vec(), data);
https://crates.io/crates/synchronized-writer
https://docs.rs/synchronized-writer