waithandle 0.1.0

A library that makes signaling between threads more ergonomic.
Documentation

waithandle-rs

A library that makes signaling between threads more ergonomic than using Condvar directly.

Inspired by the .NET System.Threading.EventWaitHandle API. Uses Condvar and Mutex under the hood to block threads without consuming CPU time.

Usage

use std::sync::Arc;
use std::time::Duration;
use waithandle::{EventWaitHandle, WaitHandle};

// Create a handle and wrap it
// in an Arc so we can share ownership
// of it with another thread.
let handle = Arc::new(EventWaitHandle::new());

// Signal a thread.
handle.signal()?;

// Did someone signal us?
handle.check()?;

// Wait for 5 seconds or until someone signals us.
if handle.wait(Duration::from_secs(5))? {
    println!("signal received");
}

Running the example

> cargo run --example simple
[WORK] Doing some work...
[WORK] Doing some work...
[WORK] Doing some work...
[WORK] Doing some work...
[WORK] Doing some work...
[WORK] Waiting...
[MAIN] Signaling thread...
[MAIN] Joining thread...
[WORK] Someone told us to shut down!
[MAIN] Done!