join

Macro join 

Source
macro_rules! join {
    () => { ... };
}
Expand description

Joins the most recent not-yet-joined thread spawned from the current thread by either go! or go_ref!.

This will always yield a go_spawn::error::Result, which indicates whether a thread was successfully joined.

ยงExamples

use go_spawn::{
    error::{JoinError, Result},
    go, go_ref, join,
};
use std::sync::{
    atomic::{AtomicI64, Ordering},
};

go_ref!(expensive_background_work());

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

go_ref! {
    for _ in 0..1_000_000 {
        COUNTER.fetch_add(1, Ordering::SeqCst);
    }
}

assert!(join!().is_ok());
assert_eq!(COUNTER.load(Ordering::SeqCst), 1_000_0000);

go!(panic!("{}", 4));

let result = join!().unwrap_err();
let expected = Some("4".to_string());
assert!(
    matches!(
        result,
        JoinError::ThreadPanic(value) if value.downcast_ref::<String>() == expected.as_ref(),
    )
);

go!(std::panic::panic_any(4_i32));

let result = join!().unwrap_err();
let expected = Some(4_i32);
assert!(
    matches!(
        result,
        JoinError::ThreadPanic(value) if value.downcast_ref::<i32>() == expected.as_ref(),
    )
);