fast_able/fast_thread_pool/
lite.rs1use std::sync::Arc;
2
3use crossbeam::atomic::AtomicCell;
4
5use super::TaskExecutor;
6
7pub struct ThreadPoolLite {
11 pub thread: TaskExecutor,
12}
13
14impl ThreadPoolLite {
15 pub fn new() -> ThreadPoolLite {
16 let use_core = super::use_last_core("thread_lite");
17 let r = ThreadPoolLite {
18 thread: TaskExecutor::new(core_affinity::CoreId { id: use_core }, 49),
19 };
20 r
21 }
22
23 pub fn spawn<F>(&self, f: F)
24 where
25 F: FnOnce(),
26 F: Send + 'static,
27 {
28 self.thread.spawn(|_| f());
29 }
30}
31
32pub fn _test_thread_lite(test_count: u128) {
33 let pool = ThreadPoolLite::new();
34 std::thread::sleep(std::time::Duration::from_millis(200));
35 let com_time = Arc::new(AtomicCell::new(0_u128));
36 for _ in 0..test_count {
37 let now = std::time::Instant::now();
38 let com_time = com_time.clone();
39 pool.spawn(move || {
40 let el = now.elapsed().as_nanos();
42 com_time.fetch_add(el);
43 });
44 }
45 println!("------------------------thread_lite 任务提交完成------------------------");
46 std::thread::sleep(std::time::Duration::from_secs(1));
47 println!(
48 "thread_lite 测试结果: 线程开启平均耗时: {:.3} micros",
49 com_time.load() as f64 / test_count as f64 / 1000.0
50 );
51}
52
53#[test]
54pub fn test_cores_count() {
55 let _ = env_logger::try_init();
57
58 let cores = crate::fast_thread_pool::CORES.clone();
60
61 let physical_cpus = num_cpus::get_physical();
63 let logical_cpus = num_cpus::get();
64
65 info!(
66 "物理CPU数量: {}, 逻辑CPU数量: {}, CORES获取的核心数: {}",
67 physical_cpus,
68 logical_cpus,
69 cores.len()
70 );
71
72 info!(
74 "核心ID列表: {:?}",
75 cores.iter().map(|x| x.id).collect::<Vec<_>>()
76 );
77
78 assert!(cores.len() > 0, "核心数应该大于0");
80 assert!(
81 cores.len() >= physical_cpus,
82 "核心数应该大于等于物理CPU数量"
83 );
84
85 if logical_cpus > physical_cpus {
87 info!("系统支持超线程技术,每个物理核心有多个逻辑核心");
88 }
89}