https://github.com/doumanash/slave-pool
Simple thread pool
https://github.com/doumanash/slave-pool
Last synced: 2 months ago
JSON representation
Simple thread pool
- Host: GitHub
- URL: https://github.com/doumanash/slave-pool
- Owner: DoumanAsh
- License: bsl-1.0
- Created: 2020-01-18T10:32:04.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-11-24T12:42:56.000Z (over 2 years ago)
- Last Synced: 2025-02-28T20:25:47.068Z (3 months ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# slave-pool
[](https://crates.io/crates/slave-pool)
[](https://docs.rs/crate/slave-pool/)
[](https://github.com/DoumanAsh/slave-pool/actions?query=workflow%3ARust)Simple thread pool
# usage
```rust
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 nowPOOL.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());
```