Struct Condvar
pub struct Condvar { /* private fields */ }
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
- No spurious wakeups: A wait will only return a non-timeout result if it
was woken up by
notify_oneornotify_all. Condvar::notify_allwill only wake up a single thread, the rest are requeued to wait for theMutexto be unlocked by the thread that was woken up.- Only requires 1 word of space, whereas the standard library boxes the
Condvardue to platform limitations. - Can be statically constructed.
- Does not require any drop glue when dropped.
- Inline fast path for the uncontended case.
Examples
use ;
use Arc;
use thread;
let pair = new;
let pair2 = pair.clone;
// Inside of our lock, spawn a new thread, and then wait for it to start
spawn;
// wait for the thread to start up
let & = &*pair;
let mut started = lock.lock;
if !*started
// Note that we used an if instead of a while loop above. This is only
// possible because parking_lot's Condvar will never spuriously wake up.
// This means that wait() will only return after notify_one or notify_all is
// called.
Implementations
impl Condvar
const fn new() -> CondvarCreates a new condition variable which is ready to be waited on and notified.
fn notify_one(&self) -> boolWakes 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
waitorwait_timeout. Calls tonotify_oneare not buffered in any way.To wake up all threads, see
notify_all().Examples
use Condvar; let condvar = new; // do something with condvar, share it with other threads if !condvar.notify_onefn notify_all(&self) -> usizeWakes 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().fn wait<T: ?Sized>(&self, mutex_guard: &mut MutexGuard<'_, T>)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 tonotify_*()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
Condvarwith a differentMutexobject.fn wait_until<T: ?Sized>(&self, mutex_guard: &mut MutexGuard<'_, T>, timeout: Instant) -> WaitTimeoutResultWaits 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 untiltimeoutis 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 preciselytimeout.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
WaitTimeoutResultvalue 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
Condvarwith a differentMutexobject.fn wait_for<T: ?Sized>(&self, mutex_guard: &mut MutexGuard<'_, T>, timeout: Duration) -> WaitTimeoutResultWaits 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 thantimeout. 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 preciselytimeout.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
WaitTimeoutResultvalue 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.fn wait_while<T, F>(&self, mutex_guard: &mut MutexGuard<'_, T>, condition: F) where T: ?Sized, F: FnMut(&mut T) -> bool,Blocks the current thread until this condition variable receives a notification. If the provided condition evaluates to
false, then the thread is no longer blocked and the operation is completed. If the condition evaluates totrue, then the thread is blocked again and waits for another notification before repeating this process.This function will atomically unlock the mutex specified (represented by
mutex_guard) and block the current thread. This means that any calls tonotify_*()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
Condvarwith a differentMutexobject.fn wait_while_until<T, F>(&self, mutex_guard: &mut MutexGuard<'_, T>, condition: F, timeout: Instant) -> WaitTimeoutResult where T: ?Sized, F: FnMut(&mut T) -> bool,Waits on this condition variable for a notification, timing out after the specified time instant. If the provided condition evaluates to
false, then the thread is no longer blocked and the operation is completed. If the condition evaluates totrue, then the thread is blocked again and waits for another notification before repeating this process.The semantics of this function are equivalent to
wait()except that the thread will be blocked roughly untiltimeoutis 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 preciselytimeout.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
WaitTimeoutResultvalue 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
Condvarwith a differentMutexobject.fn wait_while_for<T: ?Sized, F>(&self, mutex_guard: &mut MutexGuard<'_, T>, condition: F, timeout: Duration) -> WaitTimeoutResult where F: FnMut(&mut T) -> bool,Waits on this condition variable for a notification, timing out after a specified duration. If the provided condition evaluates to
false, then the thread is no longer blocked and the operation is completed. If the condition evaluates totrue, then the thread is blocked again and waits for another notification before repeating this process.The semantics of this function are equivalent to
wait()except that the thread will be blocked for roughly no longer thantimeout. 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 preciselytimeout.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
WaitTimeoutResultvalue 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.
Trait Implementations
impl Debug for Condvar
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Condvar
fn default() -> Condvar
Auto Trait Implementations
impl !Freeze for Condvar
impl RefUnwindSafe for Condvar
impl Send for Condvar
impl Sync for Condvar
impl Unpin for Condvar
impl UnsafeUnpin for Condvar
impl UnwindSafe for Condvar
Blanket Implementations
impl<T> Any for Condvar
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Condvar
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Condvar
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Condvar
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for Condvar
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Condvar
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Condvar
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>