#thread-pool #scoped #state #pool #stateful

yanked scoped_stateful_threadpool

一个用于作用域内和缓存的具有状态的线程池的库

使用旧的 Rust 2015

0.1.8 2017年10月22日

#44 in #stateful

MIT 许可证

20KB
368 lines

scoped-stateful-threadpool-rs

一个用于作用域内和缓存的线程池库 该库保持状态,因此您不需要连接池

入门

scoped-threadpool-rs 可在 crates.io 上找到。将以下依赖项添加到您的 Cargo 清单中,以获取 0.1 分支的最新版本

[dependencies]

scoped_stateful_threadpool = "0.1.*"

要始终获取最新版本,请将此 git 仓库添加到您的 Cargo 清单中

[dependencies.scoped_stateful_threadpool]
git = "https://github.com/njaard/scoped-stateful-threadpool-rs"

示例

extern crate scoped_stateful_threadpool;
use scoped_stateful_threadpool::Pool;

fn main() {
    // Create a threadpool holding 4 threads
    let mut pool = Pool::new(4, &|| 0);

    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
    pool.scoped(|scoped| {
        // Create references to each element in the vector ...
        for e in &mut vec {
            // ... and add 1 to it in a seperate thread
            scoped.execute(move |state| {
                *state += 1; // I can change the state
                *e += 1;
            });
        }
    });

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

无运行时依赖