2 个版本
使用旧的 Rust 2015
0.4.1 | 2017年10月28日 |
---|---|
0.4.0 | 2017年10月28日 |
0.3.0 |
|
0.2.0 |
|
#642 in 并发
14KB
183 行
SyncSplitter
SyncSplitter
允许多个线程同时分割可变切片。
它有点像一个 Sync
竞技场,你可以在同一个 Vec
(或某些其他可变切片)中并行'分配'元素,然后在最后取回这些元素。
你需要这样东西来并行构建树或图(例如,使用 rayon),而不必单独为每个节点分配。动机案例是一个 BVH 实现。
示例
use sync_splitter::SyncSplitter;
// We'll build a binary tree and store it in an array where each node points to its first,
// child, with the two children always adjacent.
#[derive(Default, Copy, Clone)]
struct Node {
// We'll store the depth in-lieu of some actual data.
height: u32,
// The index of the first child if not a leaf. The second will be `first_child_index + 1`.
first_child_index: Option<usize>,
}
fn create_children(parent: &mut Node, splitter: &SyncSplitter<Node>, height: u32) {
if height == 0 {
return;
}
// Calling `pop_two` (or `pop_n`) gets two consecutive elements from the original slice.
let ((left, right), first_child_index) = splitter.pop_two().expect("arena too small");
*parent = Node {
height,
first_child_index: Some(first_child_index),
};
rayon::join(|| create_children(left, splitter, height - 1),
|| create_children(right, splitter, height - 1))
}
let mut arena = vec![Node::default(); 500];
let num_nodes = {
let splitter = SyncSplitter::new(&mut arena);
{
let (root, _) = splitter.pop().expect("arena too small");
create_children(root, &splitter, 5);
}
splitter.done()
};
assert_eq!(num_nodes, 63);
arena.truncate(num_nodes);
// `arena` now contains all the nodes in our binary tree.