| Crates.io | slave-pool |
| lib.rs | slave-pool |
| version | 0.4.0 |
| created_at | 2020-01-18 11:51:34.579378+00 |
| updated_at | 2025-07-28 13:19:55.11713+00 |
| description | Simple thread pool |
| homepage | |
| repository | https://github.com/DoumanAsh/slave-pool |
| max_upload_size | |
| id | 199698 |
| size | 45,027 |
Simple thread pool
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 idx in 0..8 {
handles.push(POOL.spawn_handle(move || {
std::thread::sleep(SECOND);
idx
}));
}
POOL.set_threads(0); //Tells to shut down threads
for (idx, handle) in handles.drain(..).enumerate() {
assert_eq!(handle.wait().unwrap(), idx) //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());
std::thread::sleep(SECOND);