Skip to main content

Crate rendezvous

Crate rendezvous 

Source
Expand description

§Easier Rendezvous Channels

A Rendezvous lets one thread wait until a group of worker threads have all reached a synchronization point. Each worker holds a RendezvousGuard; once every guard is dropped, the waiting Rendezvous::rendezvous call (or its async/timeout variants) proceeds.

Internally this is a guard counter protected by a Mutex and a Condvar (plus a tokio::sync::Notify when the tokio feature is enabled). The waiter never relinquishes its own handle, so forking a guard always works — even after a timed-out wait — and a timeout never leaves the Rendezvous in a state where dropping it blocks forever.

§Crate Features

§Example usage

use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use rendezvous::{Rendezvous, RendezvousGuard};

/// A slow worker function. Sleeps, then mutates a value.
fn slow_worker_fn(_guard: RendezvousGuard, mut value: Arc<Mutex<u32>>) {
    thread::sleep(Duration::from_millis(400));
    let mut value = value.lock().unwrap();
    *value = 42;
}

fn example() {
    // The guard that ensures synchronization across threads.
    // Rendezvous itself acts as a guard: If not explicitly dropped, it will block the current
    // scope until all rendezvous points are reached.
    let rendezvous = Rendezvous::new();

    // A value to mutate in a different thread.
    let value = Arc::new(Mutex::new(0u32));

    // Run the worker in a thread.
    thread::spawn({
        let guard = rendezvous.fork_guard();
        let value = value.clone();
        move || slow_worker_fn(guard, value)
    });

    // Block until the thread has finished its work.
    rendezvous.rendezvous();

    // The thread finished in time.
    assert_eq!(*(value.lock().unwrap()), 42);
}

Structs§

Rendezvous
Rendezvous is a synchronization primitive that allows a thread to wait until a group of worker threads have all reached a certain point in the code before proceeding.
RendezvousGuard
A guard forked off a Rendezvous struct. While it is alive it keeps the owning Rendezvous from completing; dropping it (or all clones of it) releases the rendezvous.

Enums§

RendezvousTimeoutError
Timeout error that may occur during a rendezvous process.