ctx-thread

Crates.ioctx-thread
lib.rsctx-thread
version0.1.1
sourcesrc
created_at2020-10-22 20:41:33.604653
updated_at2020-10-22 20:55:15.684861
descriptionCooperative, scoped threads based on crossbeam and golang's [context](https://golang.org/pkg/context/) package.
homepage
repositoryhttps://github.com/kaiserkarel/context-thread/
max_upload_size
id304467
size22,522
Karel L. Kubat (KaiserKarel)

documentation

README

tests Actions Status Cargo Documentation

Context-thread

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.

Scope

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

use ctx_thread::scope;

let people = vec![
    "Alice".to_string(),
    "Bob".to_string(),
    "Carol".to_string(),
];

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

Context

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| {
        ctx.cancel();
    });
}).unwrap();

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

Commit count: 4

cargo fmt