Skip to content

Commit 3abfd53

Browse files
committed
Update ThreadPool
The ThredPool object now works. The threads are managed in queue and activate at the correct times.
1 parent 33771ae commit 3abfd53

1 file changed

Lines changed: 23 additions & 27 deletions

File tree

src/main.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
extern crate mapmaking;
2-
pub mod threadpool;
2+
// pub mod threadpool;
3+
pub mod threadpool2;
4+
use colored::Colorize;
5+
use threadpool2::ThreadPool;
6+
37

48
use std::{slice::SliceIndex, sync::{Arc, Barrier, atomic::AtomicUsize, mpsc::channel}, thread, time::Duration};
59
use clap::{App, Arg};
@@ -25,48 +29,40 @@ fn main() {
2529
.arg(Arg::with_name("mc_id").short("i").long("mc_id").takes_value(true).help("Returns the map at the `mc_id` Monte Carlo iteration"))
2630
.get_matches();
2731

28-
const NUM_THREADS: usize = 11;
29-
const NUM_MC_ITER: usize = 12;
30-
let th_pool = threadpool::Builder::new().num_threads(NUM_THREADS).thread_name(String::from("MapMaker MC threads")).build();
31-
//let th_pool = rayon::ThreadPoolBuilder::new().num_threads(NUM_THREADS).build().unwrap();
32+
const NUM_THREADS: usize = 6;
33+
const NUM_MC_ITER: usize = 50;
34+
// let th_pool = threadpool::Builder::new().num_threads(NUM_THREADS).thread_name(String::from("MapMaker MC threads")).build();
35+
// let th_pool = rayon::ThreadPoolBuilder::new().num_threads(NUM_THREADS).build().unwrap();
36+
let my_pool = ThreadPool::new(NUM_THREADS);
37+
3238

39+
let mc_id_set = ["000000", "000001", "000002", "000003", "000004", "000005", "000006", "000007", "000008", "000009", "000010", "000011",
40+
"000012", "000013", "000014", "000015", "000016", "000017", "000018", "000019", "000020", "000021", "000022", "000023",
41+
"000024", "000025", "000026", "000027", "000028", "000029", "000030", "000031", "000032", "000033", "000034", "000035",
42+
"000036", "000037", "000038", "000039", "000040", "000041", "000042", "000043", "000044", "000045", "000046", "000047",
43+
"000048", "000049"];
3344

34-
let mc_id_set = ["000000", "000001", "000002", "000003", "000004", "000005", "000006", "000007", "000008", "000009", "000010", "000011"];
35-
//["000012", "000013", "000014", "000015", "000016", "000017", "000018", "000019", "000020", "000021", "000022", "000023"];
36-
//["000024", "000025", "000026", "000027", "000028", "000029", "000030", "000031", "000032", "000033", "000034", "000035"];
37-
//["000036", "000037", "000038", "000039", "000040", "000041", "000042", "000043", "000044", "000045", "000046", "000047"];
38-
//["000048", "000049"];
3945
let directory_path = program.value_of("tod_path").unwrap();
4046

41-
let (tx, rx) = channel();
47+
//let (tx, rx) = channel(); (possiamo far tornare cose al thread 0, in modo tale da tener traccia di quello che sta iniziando/finendo)
4248

4349
for _th in 0..NUM_MC_ITER {
44-
let tx = tx.clone();
4550
let path = String::from(directory_path.clone());
46-
th_pool.execute(move || {
51+
let id = mc_id_set[_th];
4752

48-
tx.send(split_mc(String::from(path), mc_id_set[_th])).unwrap();
53+
my_pool.execute(move || {
54+
split_mc(path, id);
55+
sleep(std::time::Duration::from_millis(500));
4956
});
5057
}
51-
println!("Active: {}", th_pool.active_count());
52-
println!("Queue: {}", th_pool.queued_count());
53-
54-
for _ in 0..NUM_MC_ITER {
55-
println!("Ricevuto: {}", rx.recv().unwrap());
56-
57-
println!("Active: {}", th_pool.active_count());
58-
println!("Queue: {}", th_pool.queued_count());
59-
println!("Queue: {}", th_pool.max_count());
60-
}
61-
62-
//th_pool.join();
63-
6458

6559
}
6660

6761
pub fn split_mc(p: String, id: &str) -> &str {
62+
println!("New thread. MC ID: {}", id.bright_green().bold());
6863
let directory_tree = DirStruct::new(Path::new(&*p), String::from("1")).unwrap();
6964
let _my_obs = directory_tree.create_observations(id);
7065
_my_obs.binning();
66+
println!("MC ID {} completed.", id.red().bold());
7167
id
7268
}

0 commit comments

Comments
 (0)