3 releases

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

#1049 in Concurrency

Download history 809/week @ 2023-11-11 1498/week @ 2023-11-18 1072/week @ 2023-11-25 628/week @ 2023-12-02 1170/week @ 2023-12-09 479/week @ 2023-12-16 422/week @ 2023-12-23 609/week @ 2023-12-30 1158/week @ 2024-01-06 2268/week @ 2024-01-13 3330/week @ 2024-01-20 1878/week @ 2024-01-27 1493/week @ 2024-02-03 971/week @ 2024-02-10 929/week @ 2024-02-17 969/week @ 2024-02-24

4,627 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

~350KB