#thread-pool #thread-safe #thread #safe #pool

safina-threadpool

Safe thread pool - ARCHIVED: Code moved to safina crate

9 releases

new 0.2.5 Oct 27, 2024
0.2.4 Apr 1, 2022
0.2.3 Mar 24, 2022
0.2.2 Feb 26, 2022
0.1.2 Dec 23, 2020

#1574 in Asynchronous

Download history 20/week @ 2024-07-07 54/week @ 2024-07-14 33/week @ 2024-07-21 57/week @ 2024-07-28 33/week @ 2024-08-04 32/week @ 2024-08-11 24/week @ 2024-08-18 51/week @ 2024-08-25 36/week @ 2024-09-01 27/week @ 2024-09-08 21/week @ 2024-09-15 47/week @ 2024-09-22 47/week @ 2024-09-29 14/week @ 2024-10-06 17/week @ 2024-10-13 25/week @ 2024-10-20

105 downloads per month
Used in 13 crates (via safina-executor)

Apache-2.0

29KB
385 lines

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 FnOnce to the pool and one of the threads will execute it
  • Automatically restarts panicked threads
  • Retries after failing to spawn a thread
  • Drop the ThreadPool struct 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

Changelog

Changelog
  • v0.2.4 - Update docs.
  • v0.2.3 - Implement From<NewThreadPoolError> and From<TryScheduleError> for std::io::Error.
  • v0.2.2 - Add ThreadPool::join and ThreadPool::try_join.
  • v0.2.1 - Improve test coverage.
  • v0.2.0
    • ThreadPool::new to return Result.
    • ThreadPool::try_schedule to return an error when it fails to restart panicked threads.
    • ThreadPool::schedule to 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 join and try_join work with Arc<ThreadPool>.
  • Log a warning when all threads panicked.
  • Update test coverage.
  • Add a public respawn_threads function.
  • 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.

No runtime deps