Struct ore::sync::Lottery [−][src]
Expand description
A synchronized resource lottery.
Controls access to a non-Sync
resource by allowing only one thread to
win the resource “lottery.” A dummy resource is constructed for threads
that lose the lottery.
Examples
struct Discarder;
impl io::Write for Discarder {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let stderr: Box<dyn io::Write + Send> = Box::new(io::stderr());
let lottery = Lottery::new(stderr, || Box::new(Discarder));
crossbeam_utils::thread::scope(|thread_scope| {
(0..5)
.into_iter()
.map(|_| {
thread_scope.spawn(|_| {
write!(lottery.draw(), "Can you hear me?");
})
})
.for_each(|handle| handle.join().unwrap());
})
.unwrap()
Fields
winner: Mutex<Option<T>>
losers: F
Implementations
Creates a new Lottery
from the specified winner object and a function
to construct loser objects.
Trait Implementations
Auto Trait Implementations
impl<T, F> RefUnwindSafe for Lottery<T, F> where
F: RefUnwindSafe,
impl<T, F> UnwindSafe for Lottery<T, F> where
F: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more