#value #thread #publish #immutability #wait #receive #publisher

published_value

Publish a value from one thread to other threads

3 releases

Uses old Rust 2015

0.1.2 Mar 6, 2019
0.1.1 Nov 11, 2018
0.1.0 Nov 11, 2018

#13 in #publisher


Used in 2 crates (via workpool)

MIT license

9KB
161 lines

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());

No runtime deps