#thread-pool #scope #holding #scoped-threadpool

threadpool_scope

A library for adding scopes to the threadpool crate

1 unstable release

0.1.0 Jul 19, 2020

#1267 in Concurrency

Download history 5/week @ 2024-07-22 9/week @ 2024-07-29 5/week @ 2024-08-05 5/week @ 2024-08-12 99/week @ 2024-08-19 47/week @ 2024-08-26 21/week @ 2024-09-02 7/week @ 2024-09-09 16/week @ 2024-09-16 35/week @ 2024-09-23 38/week @ 2024-09-30 23/week @ 2024-10-07 46/week @ 2024-10-14 67/week @ 2024-10-21 15/week @ 2024-10-28 12/week @ 2024-11-04

140 downloads per month
Used in syncthreads

MIT license

24KB
392 lines

threadpool_scope

A library for adding scopes to threadpools. Essentially scoped_threadpool, but adapted for use with threadpool.

For more details, see the docs.

Note: the threadpool crate is expected to gain scope functionality in the next major version (2.0), which should obviate the need for this crate.

Getting Started

threadpool_scope is available on crates.io. Add the following dependency to your Cargo manifest to get the latest version of the 0.1 branch:

[dependencies]
threadpool_scope = "0.1.*"

Example

use threadpool::ThreadPool;
use threadpool_scope::scope_with;

fn main() {
    // Create a threadpool holding 4 threads
    let mut pool = ThreadPool::new(4);

    let mut vec = vec![0, 1, 2, 3, 4, 5, 6, 7];

    // Use the threads as scoped threads that can
    // reference anything outside this closure
    scope_with(&pool, |scope| {
        // Create references to each element in the vector ...
        for e in &mut vec {
            // ... and add 1 to it in a seperate thread
            scope.execute(move || {
                *e += 1;
            });
        }
    });

    assert_eq!(vec, vec![1, 2, 3, 4, 5, 6, 7, 8]);
}

Dependencies

~2MB
~40K SLoC