#thread-pool #sync #tread

bin+lib th_pool

create thread pool

9 releases

0.1.8 Aug 17, 2022
0.1.7 Aug 14, 2022
0.1.6 Jul 10, 2022
0.1.1 Jun 24, 2022

#1558 in Development tools

MIT license

18KB
141 lines

create thread-pool

[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();
}

No runtime deps