#thread-pool #scoped #flexible #scope #static #execute #wait

scoped-pool

A flexible thread pool providing scoped threads

12 releases (1 stable)

Uses old Rust 2015

1.0.0 Jul 27, 2016
0.1.10 Jul 27, 2016
0.1.9 Mar 31, 2016
0.1.8 Feb 23, 2016
0.1.4 Jan 27, 2016

#771 in Concurrency

Download history 102/week @ 2023-12-01 147/week @ 2023-12-08 211/week @ 2023-12-15 241/week @ 2023-12-22 118/week @ 2023-12-29 238/week @ 2024-01-05 171/week @ 2024-01-12 178/week @ 2024-01-19 178/week @ 2024-01-26 183/week @ 2024-02-02 203/week @ 2024-02-09 274/week @ 2024-02-16 326/week @ 2024-02-23 209/week @ 2024-03-01 342/week @ 2024-03-08 375/week @ 2024-03-15

1,286 downloads per month
Used in 15 crates (13 directly)

MIT license

24KB
468 lines

scoped-pool

A flexible thread pool providing scoped and static threads.

Documentation

Overview

Example of simple use (applying a transformation to every item in an array):

extern crate scoped_pool;

use scoped_pool::Pool;

let pool = Pool::new(4);

let mut buf = [0, 0, 0, 0];

pool.scoped(|scope| {
    for i in &mut buf {
        scope.execute(move || *i += 1);
    }
});

assert_eq!(&buf, &[1, 1, 1, 1]);

Besides the core API used above (Pool::new, Pool::scoped) this crate also provides many extremely useful convenience functions for complex code using scoped threads.

Also includes the raw WaitGroup type, which can be used to implement similar "wait for a group of actions to complete" logic, and is used in Pool and Scope.

See the generated documentation (linked above) for details.

Unique Aspects

Unlike many other scoped threadpool crates, this crate is designed to be maximally flexible: Pool and Scope are both Send + Sync, Pool is Clone, and both have many useful conveniences such as:

Pool::spawn for spawning 'static jobs.

Pool::expand for expanding the number of threads in the pool.

Pool::shutdown for shutting down the pool.

Scope::forever and Scope::zoom for externalizing Scope management and allowing fine-grained control over when jobs are scheduled and waited on.

Nearly all methods on both types require only an immutable borrow, and thus are safe to use concurrently without external synchronization.

In addition, the internal design of this crate is carefully constructed so that all unsafety is encapsulated in the Scope type, which effectively just adds lifetime scoping to the WaitGroup type for jobs scheduled on a Pool.

Usage

Use the crates.io repository; add this to your Cargo.toml along with the rest of your dependencies:

[dependencies]
scoped-pool = "0.1"

Author

Jonathan Reem is the primary author and maintainer of scoped-pool.

License

MIT

Dependencies

~40KB