#thread #spawn #go #join

go-spawn

a library that provides macros to spawn and join threads with minimal boilerplate

3 releases

0.1.2 Mar 26, 2022
0.1.1 Mar 26, 2022
0.1.0 Mar 26, 2022

#816 in Concurrency

Download history 18/week @ 2023-10-29 10/week @ 2023-11-05 13/week @ 2023-11-12 11/week @ 2023-11-19 22/week @ 2023-11-26 9/week @ 2023-12-03 9/week @ 2023-12-10 15/week @ 2023-12-17 16/week @ 2023-12-24 14/week @ 2023-12-31 17/week @ 2024-01-07 20/week @ 2024-01-14 18/week @ 2024-01-21 18/week @ 2024-01-28 15/week @ 2024-02-04 26/week @ 2024-02-11

81 downloads per month
Used in yuxii

Apache-2.0

20KB
334 lines

go-spawn

go-spawn is a library that provides macros to spawn and join threads with minimal boilerplate. Threads can be spawned with either go! or go_ref! and can be joined with either join! or join_all!.

Installation

go-spawn is published on https://crates.io. To use in your Rust project, add the latest version of go-spawn to your Cargo.toml file:

[dependencies]
go-spawn = "0.1.0"

Examples.

use go_spawn::{go, join};
use std::sync::{
    atomic::{AtomicI64, Ordering},
    Arc,
};

let counter = Arc::new(AtomicI64::new(0));
let counter_cloned = counter.clone();

// Spawn a thread that captures values by move.
go! {
    for _ in 0..100 {
        counter_cloned.fetch_add(1, Ordering::SeqCst));
    }
}

// Join the most recent thread spawned by `go_spawn` that has not yet been joined.
assert!(join!().is_ok());
assert_eq!(counter.load(Ordering::SeqCst), 100);
use go_spawn::{go_ref, join};
use std::sync::{
    atomic::{AtomicI64, Ordering},
};

static COUNTER: AtomicI64 = AtomicI64::new(0);

for _ in 0..10 {
    // Spawn a thread that captures by reference.
    go_ref!(COUNTER.fetch_add(1, Ordering::SeqCst));
}

// Join all not-yet-joined threads spawned by `go_spawn`.
join_all!();
assert_eq!(counter.load(Ordering::SeqCst), 10);

Documentation

Documentation can be found at https://docs.rs/go-spawn.

Dependencies

~0.4–32MB
~431K SLoC