百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
K

kirk

> 编程语言
开源

一个高度灵活的线程池,用于 Rust

180 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个高度灵活的线程池,用于 Rust

kirk, a highly-flexible thread pool for Rust

Documentation

The documentation is hosted on GitHub Pages.

Stability

The API and internal designs are subject to rapid change, so breakages could happen frequently until a pleasant and high-performance interface is found. Play around with this and figure out ways to improve it, but don't rely on it yet for anything truly important.

Examples

Pools are generic not only to the jobs performed, but also to the method for giving jobs to workers. They may also be scoped, allowing safe access to data in the stack of the originating thread.

…

Design

I was inspired to start this after attending the January 2016 Bay Area Rust meetup and watching Aaron Turon's presentation about crossbeam. The initial version of this crate supported only boxed, dynamically-dispatched tasks (closures). At this stage, it was essentially scoped_threadpool with a different task-passing mechanism.

The first breakthrough came when I realized that a simple Job trait would make both static and dynamic dispatch possible:

rust
pub trait Job: Send {
    fn perform(self);
}

pub struct Task(Box);

impl Job for Task {
    #[inline]
    fn perform(self) {
        let Task(task) = self;
        task.call_box();
    }
}

impl From for Task where F: FnOnce() + Send + 'a
{
    #[inline]
    fn from(f: F) -> Task {
        Task(Box::new(f))
    }
}

Much better: static dispatch with no additional allocation, while maintaining convenient dynamic dispatch. I soon added support for 'static jobs that run outside a crossbeam::scope.

At this point, I was pretty happy with the result. However, scoped_threadpool, with its simple Arc> method for delivering tasks to worker threads, stayed in the back of my mind. Eventually I decided to write benchmarks comparing the two, but since it didn't support static dispatch, I wrote a parallel set of pool and worker types. That's when the next insight came: the pool implementations were highly similar, except for some book-keeping related to how each implemented job communication. So I factored out those details into a family of traits:

rust
pub trait Crew {
    type Job: Job;
    type Member: Worker;
    type Settings: Parameters;

    fn new(settings: Self::Settings) -> Self;
    fn hire(&mut self) -> Self::Member;
    fn give(&mut self, job: F) where Self::Job: From;
    fn stop(&mut self);
}

pub trait Worker: Send {
    fn run(&mut self);
}

pub trait Parameters: Copy + Clone {
    fn num_workers(&self) -> usize;
}

The implementation of Pool became rather straightforward:

rust
pub struct Pool
    where C: Crew
{
    crew: C,
}

impl Pool
    where J: Job + 'scope,
          C: Crew + 'scope
{
    pub fn scoped(scope: &Scope, settings: C::Settings) -> Pool {
        let mut crew = C::new(settings);
        for _ in 0..settings.num_workers() {
            let mut worker = crew.hire();
            scope.spawn(move || {
                worker.run();
            });
        }
        Pool { crew: crew }
    }

    pub fn push(&mut self, f: F)
        where J: From
    {
        self.crew.give(f);
    }
}

The icing on the cake: this crate never uses unsafe:

$ grep -r --include=*.rs unsafe .
$

As dbaupp pointed out, however, the current design has a major drawback compared to other scoped threadpools because of this, since it cannot reuse the same pool for different scopes throughout the lifetime of an application.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言