slave-pool

Crates.ioslave-pool
lib.rsslave-pool
version0.2.3
sourcesrc
created_at2020-01-18 11:51:34.579378
updated_at2020-11-21 15:56:27.990873
descriptionSimple thread pool
homepage
repositoryhttps://github.com/DoumanAsh/slave-pool
max_upload_size
id199698
size20,519
Douman (DoumanAsh)

documentation

README

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());
Commit count: 12

cargo fmt