Crate sync_splitter [−] [src]
A SyncSplitter
allows multiple threads to split a mutable slice at the same time.
See the module docs for more information.
This is handy when you're building a tree (or some other graph) with multiple threads and you
want all the node to live in the same array once built, like a Sync
arena allocator.
Example
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.
Structs
SyncSplitter |
A |