Expand description

crates.io version license: Apache 2.0 unsafe forbidden pipeline status

A threadpool.

You can use it alone or with safina, a safe async runtime.

Features

  • Add closures or FnOnce to the pool
  • One of the pool’s threads will execute it
  • Automatically restarts panicked threads
  • Handles hitting process thread limit.
  • forbid(unsafe_code)
  • Depends only on std
  • 100% test coverage

Limitations

  • Allocates memory
  • Not optimized

Documentation

https://docs.rs/safina-threadpool

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

  • 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

  • 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.

Release Process

  1. Edit Cargo.toml and bump version number.
  2. Run ./release.sh

Structs

A collection of threads and a queue for jobs (FnOnce structs) they execute.

Enums