1 个不稳定版本
0.1.0 | 2021 年 10 月 10 日 |
---|
#2928 在 Rust 模式
25 每月下载
在 par-stream 中使用
25KB
552 行
concurrrent-slice
此 crate 通过提供并发处理方法扩展切片类型。
示例
slice.concurrent_chunks(chunk_size)
将任何拥有的类似切片类型(如 Vec<T>
)分割成大致相等的块。每个块在单独的线程中填充一个常量。然后从块提供的守卫中恢复原始切片。
let data: Vec<_> = vec![0u32; 12];
// Divide the vec into three chunks, each has length 4.
let mut chunks = data.concurrent_chunks(4);
let mut chunk1 = chunks.next().unwrap();
let mut chunk2 = chunks.next().unwrap();
let mut chunk3 = chunks.next().unwrap();
// Keeps the guard that will be used to recover the data.
let guard = chunks.guard();
// Process each chunk concurrently.
let handle1 = std::thread::spawn(move || {
chunk1.iter_mut().for_each(|elem| {
*elem = 1;
});
});
let handle2 = std::thread::spawn(move || {
chunk2.iter_mut().for_each(|elem| {
*elem = 2;
});
});
let handle3 = std::thread::spawn(move || {
chunk3.iter_mut().for_each(|elem| {
*elem = 3;
});
});
handle1.join().unwrap();
handle2.join().unwrap();
handle3.join().unwrap();
// We drop the chunks iterator to make sure the guard is the only reference to data.
drop(chunks);
// Recover the data.
let data = guard.unwrap();
assert_eq!(&data, &[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]);
许可证
MIT 许可证。请参阅 LICENSE 文件。
依赖项
~155KB