Expand description
§published_value
published_value allows one thread to “publish” a value to a number of
other threads. The other threads can wait for the value to be published and
receive an immutable reference to the value once it is. Once published the
value is immutable. Requests for the value after it’s been published are
efficient and do not require obtaining a lock.
§Examples
let (publisher, waiter) = published_value::new();
let thread1 = std::thread::spawn({
let waiter = waiter.clone();
move || {
let value = waiter.wait_for_value();
format!("thread1 received value {}", value)
}
});
let thread2 = std::thread::spawn({
let waiter = waiter.clone();
move || {
let value = waiter.wait_for_value();
format!("thread2 received value {}", value)
}
});
publisher.publish(42);
assert_eq!(thread1.join().unwrap(), "thread1 received value 42".to_string());
assert_eq!(thread2.join().unwrap(), "thread2 received value 42".to_string());Structs§
- Publisher
Publisherpublishes an immutable value to waiters.- Waiter
Waiterwaits for a value that has been published by Publisher. A Waiter can be cloned and cloned instances all receive the same published value.
Functions§
- new
- Creates a new published value. Returns a Publisher and Waiter pair. The
PublisherandWaiterare safe to send to different threads. Waiters can be cloned and each instance of the clone will receive the published value.