13 个稳定版本
1.1.11 | 2022 年 3 月 19 日 |
---|---|
1.1.10 | 2021 年 4 月 22 日 |
1.1.9 | 2020 年 7 月 29 日 |
1.1.7 | 2019 年 9 月 17 日 |
1.1.1 | 2018 年 11 月 14 日 |
在 异步 中排名第 163
每月下载量 4,113
用于 2 crates
7KB
73 行
Synchronized Writer
一个用于同步写入数据的简易实现。
示例
SynchronizedWriter
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());
SynchronizedOptionWriter
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);
Crates.io
https://crates.io/crates/synchronized-writer
文档
https://docs.rs/synchronized-writer