#thread-pool #thread #manager #parallel #pool

线程管理器

一个用于高效线程池和并行任务执行的简化 Rust 库,设计用于简洁、灵活和性能

27 个版本 (1 个稳定版)

1.0.0 2024 年 2 月 15 日
0.10.0 2024 年 2 月 14 日
0.9.13 2024 年 2 月 14 日

#128并发


zbusdg 中使用

MIT 许可证

51KB
1K SLoC

线程管理器

线程管理器是一个用于高效线程池和并行任务执行的简化 Rust 库,设计用于简洁、灵活和性能。

它旨在抽象出线程管理的复杂性,并提供一个方便的接口,用于并行化工作负载和检索结果。

添加到 Cargo.toml

thread-manager = "1.0"

功能

  • 作业提交:轻松提交并行执行作业,然后高效地分配给工作线程以获得最佳性能。

  • 结果检索:您可以在执行过程中通过立即获取可用的结果或在每个作业完成后提供结果来检索结果。此过程还允许在迭代结果时提交更多作业!

  • 池大小调整:在执行过程中可以调整线程管理器的大小,以根据当前工作负载优化资源分配。

  • 线程监控:通过详细的洞察力跟踪您的线程管理器,包括线程活动、工作负载分布等。

  • 优雅终止:支持优雅地终止工作线程,确保在关闭之前完成当前正在执行的作业。


使用方法

基本使用

use thread_manager::ThreadManager;

fn main() {
    // Create ThreadManager with 4 worker threads
    // ::<T> specifies return type for jobs
    let mut thread_manager = ThreadManager::<()>::new(4);

    // Submit job for execution
    thread_manager.execute(|| {
        // Your job logic here
    });

    // Optional ways to proceed after executing a job
    //
    // Resize the number of worker threads
    thread_manager.resize(6);

    // Wait for all worker threads to complete
    thread_manager.join();

    // Terminate all worker threads gracefully and join
    thread_manager.terminate_all();
}

检索结果

use thread_manager::ThreadManager;

fn main() {
    // Create ThreadManager with 4 worker threads
    // ::<T> specifies return type for jobs
    let mut thread_manager = ThreadManager::<f32>::new(4);

    // Submit job for execution
    thread_manager.execute(|| {
        return 50.0 / 32.0;
    });

    // The ResultIter retrieves all the available results without blocking
    for result in thread_manager.results() {
        println!("{}", result);
    }

    // The YieldResultIter blocks if there are jobs in the queue
    // This way the 'for loop' only completes when all jobs are executed
    for result in thread_manager.yield_results() {
        println!("{}", result);
        // You can execute jobs while iterating over the results
        // Beware that it will run indefinitely if there is no condition for execution
        // As it will execute and yield a result in the same loop
    }
}

监控状态和作业信息

use thread_manager::ThreadManager;

fn main() {
    // ... Create thread manager and execute jobs

    // Worker threads that could be busy or waiting
    let active_threads: usize = thread_manager.active_threads();

    // Worker threads that are busy and executing a job
    let busy_threads: usize = thread_manager.busy_threads();

    // Worker threads that are waiting to receive a job
    let waiting_threads: usize = thread_manager.waiting_threads();

    // The amount of jobs left in the queue
    let job_queue: usize = thread_manager.job_queue();

    // The job distribution of execution across worker threads
    // Example distribution of 4 worker threads:
    // [4, 3, 3, 3] => each value is the amount of jobs executed for each worker
    let job_distribution: Vec<usize> = thread_manager.job_distribution();

    // The total amount of jobs received across worker threads
    let received_jobs: usize = thread_manager.received_jobs();

    // The total amount of jobs sent across worker threads
    let sent_jobs: usize = thread_manager.sent_jobs();

    // The total amount of jobs concluded across worker threads
    let concluded_jobs: usize = thread_manager.concluded_jobs();
}

待办事项

  • — 添加文档

许可证

This project is licensed under the MIT License.
See the LICENSE file for more information.

依赖关系

~350KB