Struct ore::sync::Lottery[][src]

pub struct Lottery<T, F> where
    F: Fn() -> T, 
{ /* fields omitted */ }
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()

Implementations

Creates a new Lottery from the specified winner object and a function to construct loser objects.

Attempts to win the lottery. It returns the winner resource if this is the first thread to call draw. If another thread has already claimed the winner resource, it instead constructs and returns a loser resource.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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