#wait #condvar #mutex #synchronization #sync

waitable

A synchronized (atomic) value container implementing the Mutex+Condvar pattern for efficient blocking waits

1 unstable release

0.1.0 Dec 27, 2022

#1050 in Concurrency

45 downloads per month

MIT license

5KB

Waitable

A synchronized (atomic) value container implementing the Mutex+Condvar pattern for efficient blocking waits

Usage

use std::sync::Arc;
use waitable::Waitable;

let w = Arc::new(Waitable::new(0));

println!("Spawning thread...");

let join_handle = {
	let w = w.clone();
	std::thread::spawn(move || {
		println!("Thread waiting...");
		w.wait(&42);
		println!("Thread done waiting");
	})
};

println!("Waiting to set...");
std::thread::sleep(std::time::Duration::from_millis(500));

println!("Setting...");
w.set(42);

join_handle.join().unwrap();
println!("All done");
Spawning thread...
Waiting to set...
Thread waiting...
Setting...
Thread done waiting
All done

lib.rs:

This crate consists of a single struct, Waitable that holds a value which can be get, set and waited upon.

Dependencies

~0.4–6.5MB
~11K SLoC