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

threadpool_scope

为 threadpool crate 添加作用域的库

1 个不稳定版本

0.1.0 2020 年 7 月 19 日

#812并发

Download history 8/week @ 2024-03-12 11/week @ 2024-03-19 3/week @ 2024-03-26 40/week @ 2024-04-02 13/week @ 2024-04-09 9/week @ 2024-04-16 21/week @ 2024-04-23 3/week @ 2024-04-30 23/week @ 2024-05-07 17/week @ 2024-05-14 14/week @ 2024-05-21 35/week @ 2024-05-28 22/week @ 2024-06-04 12/week @ 2024-06-11 14/week @ 2024-06-18 35/week @ 2024-06-25

87 每月下载次数
syncthreads 中使用

MIT 许可协议

24KB
392 代码行

threadpool_scope

为 threadpools 添加作用域的库。本质上类似于 scoped_threadpool,但针对 threadpool 进行了适配。

更多详情,请参阅 文档

注意:threadpool crate 预计将在下一个主要版本(2.0)中获得作用域功能,这将消除对当前库的需求。

入门指南

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

[dependencies]
threadpool_scope = "0.1.*"

示例

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]);
}

依赖项

~2MB
~40K SLoC