13 个版本
0.2.9 | 2024 年 5 月 20 日 |
---|---|
0.2.8 | 2024 年 3 月 25 日 |
0.2.7 | 2024 年 1 月 8 日 |
0.2.6 | 2023 年 3 月 14 日 |
0.1.1 | 2021 年 7 月 30 日 |
#165 在 并发
每月 216 次下载
在 2 个 Crates 中使用 (通过 assets_manager)
43KB
1K SLoC
Sync File
可并发读取的文件。
std::fs::File
是 Sync
,但从中并发读取会导致竞争条件,因为操作系统有一个单独的光标,该光标被多个线程使用。
SyncFile
通过使用特定于平台的扩展来进行位置 I/O,以解决这个问题,因此文件的光标不会被共享。
示例
use std::io::Read;
use sync_file::SyncFile;
/// Reads a file byte by byte.
/// Don't do this in real code !
fn read_all<R: Read>(mut file: R) -> std::io::Result<Vec<u8>> {
let mut result = Vec::new();
let mut buf = [0];
while file.read(&mut buf)? != 0 {
result.extend(&buf);
}
Ok(result)
}
// Open a file
let f = SyncFile::open("hello.txt")?;
let f_clone = f.clone();
// Read it concurrently
let thread = std::thread::spawn(move || read_all(f_clone));
let res1 = read_all(f)?;
let res2 = thread.join().unwrap()?;
// Both clones read the whole content
// This would not work with `std::fs::File`
assert_eq!(res1, b"Hello World!\n");
assert_eq!(res2, b"Hello World!\n");
许可证
根据以下任一项许可:
- Apache 许可证 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
贡献
除非您明确表示,否则您提交给作品以包含在内的任何贡献,根据 Apache-2.0 许可证定义,应双许可如上所述,不附加任何额外条款或条件。
依赖关系
~175KB