# Simple threadpool
My own implementation of [The Rust programming langauge official book implementation of a threadpool](https://doc.rust-lang.org/book/ch16-01-threads.html).
## Features
- Configurable pool size (single-threaded, multi-threaded, or auto-detect)
- Graceful shutdown with job completion guarantees
- Worker signaling - jobs can signal all workers to stop via `Arc<AtomicBool>`
## Usage
### Basic Example
```rust
use jlizard_simple_threadpool::threadpool::ThreadPool;
// let pool = ThreadPool::new(4); // 4 workers, or 0 for auto-detect, 1 for single-threaded
let pool = ThreadPool::default(); // runs the pool while auto detecting maximum threads
}).expect("Failed to execute");
```
### Worker Signaling (Early Termination)
For scenarios like hash collision detection where one worker should stop all others:
```rust
use jlizard_simple_threadpool::threadpool::ThreadPool;
use std::sync::{Arc, atomic::Ordering};
let pool = Arc::new(ThreadPool::default());
for i in 0..1000 {
let pool_clone = Arc::clone(&pool);
pool.execute(move || {
if collision_detected() {
pool_clone.signal_stop(); // Stop all workers
}
}).unwrap();
}
```
Or check the signal within jobs:
```rust
use jlizard_simple_threadpool::threadpool::ThreadPool;
let pool = Threadpool::default();
let kill_signal = pool.get_kill_signal();
if kill_signal.load(Ordering::Relaxed) {
return; // Stop early
}
// Do work...
}
}).unwrap();
```
## API
- `new(size: u8)` - Create pool (0=auto, 1=single-threaded, N=N workers)
- `execute<F>(&self, f: F)` - Execute job
- `signal_stop(&self)` - Signal all workers to stop after current job
- `get_kill_signal(&self)` - Get `Arc<AtomicBool>` to check/set from jobs
- `is_single_threaded(&self)` - Check if single-threaded mode
## Example Implementation
[vex2pdf usage](https://gitlab.com/jurassicLizard/vex2pdf/-/blob/master/src/files_proc/processor.rs?ref_type=heads#L165-197)
## License
[The Unlicense](./LICENSE)