SHARE
TWEET

Untitled

a guest Sep 16th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use rayon::prelude::*; // 1.1.0
  2. use parking_lot::Mutex; // 0.9.0
  3.  
  4. fn main() {
  5.  
  6.     let vecs = vec![
  7.         Mutex::new(vec![1, 2, 3]),
  8.         Mutex::new(vec![1, 2, 3]),
  9.         Mutex::new(vec![1, 2, 3]),
  10.         Mutex::new(vec![1, 2, 3]),
  11.         Mutex::new(vec![1, 2, 3]),
  12.         Mutex::new(vec![1, 2, 3]),
  13.     ];
  14.    
  15.     let mut indices = vec![4, 3, 1];
  16.    
  17.     // Ensure all indices are unique to ensure safety:
  18.     indices.sort_unstable();
  19.     let len = indices.len();
  20.     indices.dedup();
  21.     assert!(indices.len() == len);
  22.    
  23.     indices.par_iter().for_each(|index| {
  24.          vecs[*index].try_lock().unwrap().push(4);
  25.     });
  26.    
  27.     println!("vecs: {:?}", vecs);
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top