Modified par_for to use split_iterator and ThreadPoolManager

This commit is contained in:
Jack Yu 2021-04-18 12:23:29 -04:00
commit bb1391b8b2
4 changed files with 84 additions and 15 deletions

View file

@ -1,6 +1,5 @@
use rand::Rng;
use rustmp::par_for;
use rustmp::sysinfo::SystemObject;
use std::time;
#[derive(Debug)]
@ -23,13 +22,10 @@ fn main() {
par_for! {
for i in 1..10, capturing numbers {
// TODO: move this to parallel macro once tid design is finalized
SystemObject::get_instance().set_affinity(i as usize - 1)
.expect("Failed to bind thread to proc!");
std::thread::sleep(
time::Duration::from_secs(
rand::thread_rng().gen_range(1..10)));
//std::thread::sleep(
// time::Duration::from_secs(
// rand::thread_rng().gen_range(1..10)));
let mut lock = numbers.write();
lock.push(Student::new(i));
println!("Thread {} running!", i);

28
src/bin/tpm_example.rs Normal file
View file

@ -0,0 +1,28 @@
use rustmp::threadpool::{ThreadPoolManager, Job, as_static_job};
use std::sync::Arc;
fn main() {
let tpm_mtx= ThreadPoolManager::get_instance_guard();
let tpm = tpm_mtx.lock().unwrap();
println!("Submitting jobs!");
let mut vector = Vec::new();
for i in 0..tpm.num_threads {
let cl = as_static_job(move || {println!("Hello from {}!", i)});
vector.push(cl);
}
tpm.exec(vector);
println!("Submitting more jobs with panic on tid=3!");
let mut vector2 = Vec::new();
for i in 0..tpm.num_threads {
let x = 9;
let cl = Arc::new(move || {
if x * i == 27 {
//panic!("Panic test");
}
}) as Job;
vector2.push(cl);
}
tpm.exec(vector2);
}