Expand description
§ARCHIVED ARCHIVED ARCHIVED
This crate is archived and will not be updated.
The code is now at
safina::threadpool in the
safina crate.
§safina-threadpool
A threadpool.
You can use it alone or with safina,
a safe async runtime.
§Features
- Add a closure or
FnOnceto the pool and one of the threads will execute it - Automatically restarts panicked threads
- Retries after failing to spawn a thread
- Drop the
ThreadPoolstruct to stop all idle threads. - Destroy the pool and wait for all threads to stop
forbid(unsafe_code)- Depends only on
std - 100% test coverage
§Limitations
- Not optimized
§Examples
let pool =
safina_threadpool::ThreadPool::new("worker", 2).unwrap();
let receiver = {
let (sender, receiver) =
std::sync::mpsc::channel();
for data in data_source {
let sender_clone = sender.clone();
pool.schedule(
move || process_data(data, sender_clone));
}
receiver
};
let results: Vec<ProcessResult> =
receiver.iter().collect();
// ...§Alternatives
blocking- Popular
- A little
unsafecode - blocking/issues/24: Recover from thread spawn failure
threadpool- Popular
- Well maintained
- Dependencies have
unsafecode - rust-threadpool/issues/97: Feature: provide a way to shutdown the thread pool
- Panics when failing to spawn a thread.
scoped_threadpool- Popular
- Contains
unsafecode - Unmaintained
- Does not restart workers on panic.
scheduled-thread-pool- Used by a popular connection pool library
- Dependencies have
unsafecode - Schedule jobs to run immediately, periodically, or after a specified delay.
workerpool- Dependencies have
unsafecode
- Dependencies have
threads_pool- Full of
unsafe
- Full of
thread-pool- Old
- Dependencies have
unsafecode
tasque- Dependencies have
unsafecode
- Dependencies have
fast-threadpool- Dependencies have
unsafecode
- Dependencies have
blocking-permit- Full of
unsafe
- Full of
rayon-core- Full of
unsafe
- Full of
§Changelog
Changelog
- v0.2.4 - Update docs.
- v0.2.3 - Implement
From<NewThreadPoolError>andFrom<TryScheduleError>forstd::io::Error. - v0.2.2 - Add
ThreadPool::joinandThreadPool::try_join. - v0.2.1 - Improve test coverage.
- v0.2.0
ThreadPool::newto returnResult.ThreadPool::try_scheduleto return an error when it fails to restart panicked threads.ThreadPool::scheduleto handle failure starting replacement threads.
- v0.1.4 - Stop threads on drop.
- v0.1.3 - Support stable Rust! Needs 1.51+.
- v0.1.2 - Add another example
- v0.1.1 - Simplified internals and improved documentation.
- v0.1.0 - First release
§TO DO
- Make
joinandtry_joinwork withArc<ThreadPool>. - Log a warning when all threads panicked.
- Update test coverage.
- Add a public
respawn_threadsfunction. - Add a stress test
- Add a benchmark. See benchmarks in https://crates.io/crates/executors
- Add a way for a job to schedule another job on the same thread, with stealing.
Structs§
- Thread
Pool - A collection of threads and a queue for jobs (
FnOncestructs) they execute.