#thread-pool #sync #thread

bin+lib th_pool

创建线程池

9 个版本

0.1.8 2022 年 8 月 17 日
0.1.7 2022 年 8 月 14 日
0.1.6 2022 年 7 月 10 日
0.1.1 2022 年 6 月 24 日

#1126 in 开发工具

MIT 许可证

18KB
141

创建线程池

[dependencies]
th_pool = "*"
fn main() {
    run();
}

fn run() {
    use th_pool::Pool;
    fn thread_owner_optimization() {
        use std::cell::RefCell;
        use std::collections::HashSet;
        use std::sync::Arc;

        let pool: Arc<Pool<RefCell<_>>> =
            Arc::new(Pool::new(Box::new(|| RefCell::new(HashSet::new()))));
        pool.get().value().borrow_mut().insert("thead".to_string());
        println!("pool::{:?}", pool.get().value().borrow());

        let pool1 = pool.clone();
        let t1 = std::thread::spawn(move || {
            let guard = pool1.get();
            let v = guard.value();
            v.borrow_mut().insert("thead".to_string());
            println!("thead_1::{:?}", v.borrow());
        });

        let pool2 = pool.clone();
        let t2 = std::thread::spawn(move || {
            let guard = pool2.get();
            let v = guard.value();
            v.borrow_mut().insert("thead".to_string());
            println!("thead_2::{:?}", v.borrow());
        });

        let pool3 = pool.clone();
        let t3 = std::thread::spawn(move || {
            let guard = pool3.get();
            let v = guard.value();
            v.borrow_mut().insert("thead".to_string());
            println!("thead_3::{:?}", v.borrow());
        });

        t1.join().unwrap();
        t2.join().unwrap();
        t3.join().unwrap();


    
    }
    thread_owner_optimization();
}

无运行时依赖