[][src]Struct parking_lot::Condvar

pub struct Condvar { /* fields omitted */ }

A Condition Variable

Condition variables represent the ability to block a thread such that it consumes no CPU time while waiting for an event to occur. Condition variables are typically associated with a boolean predicate (a condition) and a mutex. The predicate is always verified inside of the mutex before determining that thread must block.

Note that this module places one additional restriction over the system condition variables: each condvar can be used with only one mutex at a time. Any attempt to use multiple mutexes on the same condition variable simultaneously will result in a runtime panic. However it is possible to switch to a different mutex if there are no threads currently waiting on the condition variable.

Differences from the standard library Condvar

Examples

use parking_lot::{Mutex, Condvar};
use std::sync::Arc;
use std::thread;

let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();

// Inside of our lock, spawn a new thread, and then wait for it to start
thread::spawn(move|| {
    let &(ref lock, ref cvar) = &*pair2;
    let mut started = lock.lock();
    *started = true;
    cvar.notify_one();
});

// wait for the thread to start up
let &(ref lock, ref cvar) = &*pair;
let mut started = lock.lock();
while !*started {
    cvar.wait(&mut started);
}

Methods

impl Condvar[src]

pub fn new() -> Condvar[src]

Creates a new condition variable which is ready to be waited on and notified.

pub fn notify_one(&self) -> bool[src]

Wakes up one blocked thread on this condvar.

Returns whether a thread was woken up.

If there is a blocked thread on this condition variable, then it will be woken up from its call to wait or wait_timeout. Calls to notify_one are not buffered in any way.

To wake up all threads, see notify_all().

Examples

use parking_lot::Condvar;

let condvar = Condvar::new();

// do something with condvar, share it with other threads

if !condvar.notify_one() {
    println!("Nobody was listening for this.");
}

pub fn notify_all(&self) -> usize[src]

Wakes up all blocked threads on this condvar.

Returns the number of threads woken up.

This method will ensure that any current waiters on the condition variable are awoken. Calls to notify_all() are not buffered in any way.

To wake up only one thread, see notify_one().

pub fn wait<T: ?Sized>(&self, mutex_guard: &mut MutexGuard<T>)[src]

Blocks the current thread until this condition variable receives a notification.

This function will atomically unlock the mutex specified (represented by mutex_guard) and block the current thread. This means that any calls to notify_*() which happen logically after the mutex is unlocked are candidates to wake this thread up. When this function call returns, the lock specified will have been re-acquired.

Panics

This function will panic if another thread is waiting on the Condvar with a different Mutex object.

pub fn wait_until<T: ?Sized>(
    &self,
    mutex_guard: &mut MutexGuard<T>,
    timeout: Instant
) -> WaitTimeoutResult
[src]

Waits on this condition variable for a notification, timing out after the specified time instant.

The semantics of this function are equivalent to wait() except that the thread will be blocked roughly until timeout is reached. This method should not be used for precise timing due to anomalies such as preemption or platform differences that may not cause the maximum amount of time waited to be precisely timeout.

Note that the best effort is made to ensure that the time waited is measured with a monotonic clock, and not affected by the changes made to the system time.

The returned WaitTimeoutResult value indicates if the timeout is known to have elapsed.

Like wait, the lock specified will be re-acquired when this function returns, regardless of whether the timeout elapsed or not.

Panics

This function will panic if another thread is waiting on the Condvar with a different Mutex object.

pub fn wait_for<T: ?Sized>(
    &self,
    mutex_guard: &mut MutexGuard<T>,
    timeout: Duration
) -> WaitTimeoutResult
[src]

Waits on this condition variable for a notification, timing out after a specified duration.

The semantics of this function are equivalent to wait() except that the thread will be blocked for roughly no longer than timeout. This method should not be used for precise timing due to anomalies such as preemption or platform differences that may not cause the maximum amount of time waited to be precisely timeout.

Note that the best effort is made to ensure that the time waited is measured with a monotonic clock, and not affected by the changes made to the system time.

The returned WaitTimeoutResult value indicates if the timeout is known to have elapsed.

Like wait, the lock specified will be re-acquired when this function returns, regardless of whether the timeout elapsed or not.

Panics

Panics if the given timeout is so large that it can't be added to the current time. This panic is not possible if the crate is built with the nightly feature, then a too large timeout becomes equivalent to just calling wait.

Trait Implementations

impl Default for Condvar[src]

impl Debug for Condvar[src]

Auto Trait Implementations

impl Send for Condvar

impl Unpin for Condvar

impl Sync for Condvar

impl UnwindSafe for Condvar

impl RefUnwindSafe for Condvar

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Erased for T[src]