3 releases

0.2.3 Nov 21, 2020
0.2.2 Jan 27, 2020
0.1.2 Jan 24, 2020

#544 in Concurrency

Download history 1461/week @ 2024-01-10 3222/week @ 2024-01-17 2317/week @ 2024-01-24 1728/week @ 2024-01-31 736/week @ 2024-02-07 875/week @ 2024-02-14 1002/week @ 2024-02-21 1344/week @ 2024-02-28 1160/week @ 2024-03-06 810/week @ 2024-03-13 942/week @ 2024-03-20 1053/week @ 2024-03-27 441/week @ 2024-04-03 909/week @ 2024-04-10 1295/week @ 2024-04-17 977/week @ 2024-04-24

3,642 downloads per month

BSL-1.0 license

20KB
382 lines

slave-pool

Crates.io Documentation Build

Simple thread pool

usage

use slave_pool::ThreadPool;
const SECOND: core::time::Duration = core::time::Duration::from_secs(1);

static POOL: ThreadPool = ThreadPool::new();

POOL.set_threads(8); //Tell how many threads you want

let mut handles = Vec::new();
for _ in 0..8 {
    handles.push(POOL.spawn_handle(|| {
        std::thread::sleep(SECOND);
    }));
}

POOL.set_threads(0); //Tells to shut down threads

for handle in handles {
    assert!(handle.wait().is_ok()) //Even though we told  it to shutdown all threads, it is going to finish queued job first
}

let handle = POOL.spawn_handle(|| {});
assert!(handle.wait_timeout(SECOND).is_err()); // All are shutdown now

POOL.set_threads(1); //But let's add one more

assert!(handle.wait().is_ok());

let handle = POOL.spawn_handle(|| panic!("Oh no!")); // We can panic, if we want

assert!(handle.wait().is_err()); // In that case we'll get error, but thread will be ok

let handle = POOL.spawn_handle(|| {});

POOL.set_threads(0);

assert!(handle.wait().is_ok());

Dependencies

~345KB