Skip to content

Commit

Permalink
Modify external data in parallel w/ Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
simsekgokhan committed May 10, 2024
1 parent 554b4da commit 1d95142
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde_json = "1"
tokio = { version = "1.23.0", features = ["full"] }

[dev-dependencies]
assert_unordered = "0.3"
criterion = { version = "0.5.1", features = ["html_reports"] }

[[bench]]
Expand Down
43 changes: 41 additions & 2 deletions src/multi_threading_par_iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
use rayon::prelude::*;
use rayon::{prelude::*, vec};
use crate::utils::pp;
use std::collections::HashSet;
use std::sync::Mutex;

#[test] fn ex1_adv_modify_ext_data() {
const ARR_SIZE: usize = 10_000;
// a. Modifying external data in parallel w/ Mutex
// let arr = [1,2,4,3];
let arr: [usize; ARR_SIZE] = core::array::from_fn(|i| i + 1);
let vv: Mutex<Vec<_>> = Mutex::new(vec![]);
let now = std::time::Instant::now();
arr.par_iter().for_each(|item| {
let mut vv = vv.lock().unwrap(); // MutexGuard<T>
vv.push(item * 10);
});
println!("(a.1) {:.2?}", now.elapsed());
let vv = vv.into_inner().unwrap(); // T
// !!! vv will be unordered
// Sort and compare unordered vectors
let now = std::time::Instant::now();
let vv: HashSet<_> = vv.into_iter().collect();
let ww: [usize; ARR_SIZE] = core::array::from_fn(|i| (i+1) * 10);
let ww: HashSet<_> = ww.into_iter().collect();
assert_eq!(vv, ww);
println!("(a.2) {:.2?}", now.elapsed());
// or
let now = std::time::Instant::now();
use assert_unordered::assert_eq_unordered;
assert_eq_unordered!(vv, ww);
println!("(a.3) {:.2?}", now.elapsed());

// b. Same op. w/o using Mutex - we have error
// error[E0596]: cannot borrow `vv` as mutable,
// as it is a captured variable in a `Fn` closure
// let mut vv: Vec<String> = vec![];
// [1,2,3].par_iter().for_each(|item| {
// vv.push(item.to_string());
// });
// println!("vv {:?}", vv);
}

fn parallel_exec(arr: &mut [i32]) {
let start_time = chrono::Utc::now().time();
Expand All @@ -17,7 +56,7 @@ fn single_thread_exec(arr: &mut [i32]) {
println!(" (b) Single thread: {} us", pp(diff));
}

#[test] fn ex1() {
#[test] fn ex2_simple() {
const SIZE_S: usize = 5_000;
println!("(1) Arr size: {}", pp(SIZE_S));
let mut small_arr: [i32; SIZE_S] = [100; SIZE_S];
Expand Down

0 comments on commit 1d95142

Please sign in to comment.