Skip to content

Commit

Permalink
More examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vigna committed Nov 25, 2024
1 parent f4c711b commit b4a43c9
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/utils/sync_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use std::cell::Cell;
/// # Examples
///
/// In this example, you can see that `SyncCell` enables mutation across
/// threads.
/// threads:
///
/// ```
/// use webgraph::utils::SyncCell;
Expand Down Expand Up @@ -82,6 +82,32 @@ use std::cell::Cell;
/// });
/// });
/// ```
///
/// In this example, we invert a permutation in parallel:
///
/// ```
/// use webgraph::utils::SyncCell;
/// use webgraph::utils::SyncSlice;
///
/// let mut perm = vec![0, 2, 3, 1];
/// let mut inv = vec![0; perm.len()];
/// let inv_sync = inv.as_sync_slice();
///
/// std::thread::scope(|scope| {
/// scope.spawn(|| { // Invert first half
/// for i in 0..2 {
/// unsafe { inv_sync[perm[i]].set(i) };
/// }
/// });
///
/// scope.spawn(|| { // Invert second half
/// for i in 2..perm.len() {
/// unsafe { inv_sync[perm[i]].set(i) };
/// }
/// });
/// });
///
/// assert_eq!(inv, vec![0, 3, 1, 2]);
#[repr(transparent)]
pub struct SyncCell<T: ?Sized>(Cell<T>);
Expand Down

0 comments on commit b4a43c9

Please sign in to comment.