Fixed ad_hoc parallel versions using 1 extra thread than specified.

This commit is contained in:
Jack Yu 2021-04-05 14:27:15 -04:00
parent 23384f7d82
commit 31fca0b6d7
1 changed files with 12 additions and 1 deletions

View File

@ -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 // Execution - Spawn threads with loop contents
for __rmp_internal_iter in __rmp_internal_iter_arr { for __rmp_internal_iter in __rmp_internal_iter_arr {
// Clone used Arcs here // 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 // Cleanup - Wait for threads
for __rmp_internal_thread in __rmp_internal_threads_arr { for __rmp_internal_thread in __rmp_internal_threads_arr {
let _ = __rmp_internal_thread.join(); let _ = __rmp_internal_thread.join();
@ -114,13 +123,15 @@ fn par_main() {
let counter = Arc::new(AtomicIsize::new(0)); let counter = Arc::new(AtomicIsize::new(0));
let mut children = vec![]; let mut children = vec![];
for i in 0..4 { for i in 1..4 {
let counter = Arc::clone(&counter); let counter = Arc::clone(&counter);
children.push(thread::spawn(move || { children.push(thread::spawn(move || {
let index = counter.fetch_add(1, Ordering::SeqCst); let index = counter.fetch_add(1, Ordering::SeqCst);
println!("Index {}: Hello from loop {}!", index, i); 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 { for child in children {
let _ = child.join(); let _ = child.join();