#thread #context #golang #scope #scoped #cooperative #crossbeam


Cooperative, scoped threads based on crossbeam and golang’s context package

2 releases

0.1.1 Oct 22, 2020
0.1.0 Oct 22, 2020

#640 in Concurrency

Custom license

235 lines

tests Actions Status Cargo Documentation


Threads that run within a context.

Most of the time, threads that outlive the parent thread are considered a code smell. ctx-thread ensures that all threads are joined before returning from the scope. Child threads have access to the Context object, which they can use to poll the status of the thread group. If one of the threads panics, the context is cancelled.


This library is based on the crossbeam's scoped threads:

use ctx_thread::scope;

let people = vec![

scope(|ctx| {
    for person in &people {
        ctx.spawn(move |_| {
            println!("Hello, {}", person);


Aside from referring to the outer scope, threads may check the extra methods and return if necessary:

use ctx_thread::scope;

scope(|ctx| {
    ctx.spawn(|ctx| {
        while ctx.active() {
            // do work

    ctx.spawn(|ctx| {

Note that these context based cancellations are a form of cooperative scheduling. Threads can still block even if a context expires.