3 个版本

0.2.3 2020年11月21日
0.2.2 2020年1月27日
0.1.2 2020年1月24日

406并发

Download history 816/week @ 2024-03-15 1217/week @ 2024-03-22 573/week @ 2024-03-29 383/week @ 2024-04-05 1365/week @ 2024-04-12 1156/week @ 2024-04-19 763/week @ 2024-04-26 507/week @ 2024-05-03 344/week @ 2024-05-10 300/week @ 2024-05-17 397/week @ 2024-05-24 420/week @ 2024-05-31 718/week @ 2024-06-07 631/week @ 2024-06-14 290/week @ 2024-06-21 121/week @ 2024-06-28

1,815 每月下载次数

BSL-1.0 许可证

20KB
382

slave-pool

Crates.io Documentation Build

简单线程池

用法

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

依赖项

~350KB