From 31fca0b6d7a4e2edd5666c3f26c5e9ecd82c6372 Mon Sep 17 00:00:00 2001 From: Yiyao Yu Date: Mon, 5 Apr 2021 14:27:15 -0400 Subject: [PATCH] Fixed ad_hoc parallel versions using 1 extra thread than specified. --- src/bin/test_ad_hoc.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/bin/test_ad_hoc.rs b/src/bin/test_ad_hoc.rs index b013313..50ed1b0 100644 --- a/src/bin/test_ad_hoc.rs +++ b/src/bin/test_ad_hoc.rs @@ -78,6 +78,9 @@ fn rmp_main() { } } + // Startup - Extract the thread's own iterator + let __rmp_internal_iter_self = __rmp_internal_iter_arr.remove(0); + // Execution - Spawn threads with loop contents for __rmp_internal_iter in __rmp_internal_iter_arr { // Clone used Arcs here @@ -96,6 +99,12 @@ fn rmp_main() { })); } + // Execution - Extract the same thread logic for self + for i in __rmp_internal_iter_self { + println!("Index {}: Hello from loop {}!", __rmp_var_counter.load(Ordering::SeqCst), i); + __rmp_var_counter.fetch_add(1, Ordering::SeqCst); + } + // Cleanup - Wait for threads for __rmp_internal_thread in __rmp_internal_threads_arr { let _ = __rmp_internal_thread.join(); @@ -114,13 +123,15 @@ fn par_main() { let counter = Arc::new(AtomicIsize::new(0)); let mut children = vec![]; - for i in 0..4 { + for i in 1..4 { let counter = Arc::clone(&counter); children.push(thread::spawn(move || { let index = counter.fetch_add(1, Ordering::SeqCst); println!("Index {}: Hello from loop {}!", index, i); })); } + let index = counter.fetch_add(1, Ordering::SeqCst); + println!("Index {}: Hello from loop {}!", index, 0); for child in children { let _ = child.join();