pub struct Condvar<C> { /* private fields */ }Expand description
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.
Implementations§
Source§impl<C: RawCondvar> Condvar<C>
impl<C: RawCondvar> Condvar<C>
Sourcepub const fn new() -> Condvar<C>
pub const fn new() -> Condvar<C>
Creates a new condition variable which is ready to be waited on and notified.
Sourcepub fn raw(&self) -> &C
pub fn raw(&self) -> &C
Returns the underlying raw condvar object.
Note that you will most likely need to import the RawCondvar trait from
lock_api to be able to call functions on the raw condvar.
Sourcepub fn notify_one(&self) -> bool
pub fn notify_one(&self) -> bool
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.
To wake up all threads, see notify_all().
Sourcepub fn notify_all(&self) -> usize
pub fn notify_all(&self) -> usize
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.
To wake up only one thread, see notify_one().
Sourcepub fn wait<T: ?Sized>(&self, mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>)
pub fn wait<T: ?Sized>(&self, mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>)
Blocks the current thread until this condition variable receives a notification.
This function will 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
The underlying RawCondvar implementation provided by C is permitted
to panic if requested to wait on two distinct MutexGuards simultaneously.
Sourcepub fn wait_while<T, F>(
&self,
mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>,
condition: F,
)
pub fn wait_while<T, F>( &self, mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>, condition: F, )
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 to true, then the thread is blocked again and
waits for another notification before repeating this process.
This function will 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
The underlying RawCondvar implementation provided by C is permitted
to panic if requested to wait on two distinct MutexGuards simultaneously.
Source§impl<R: RawMutexTimed, C: RawCondvarTimed<RawMutex = R>> Condvar<C>
impl<R: RawMutexTimed, C: RawCondvarTimed<RawMutex = R>> Condvar<C>
Sourcepub fn wait_until<T: ?Sized>(
&self,
mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>,
timeout: <C::RawMutex as RawMutexTimed>::Instant,
) -> WaitTimeoutResult
pub fn wait_until<T: ?Sized>( &self, mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>, timeout: <C::RawMutex as RawMutexTimed>::Instant, ) -> WaitTimeoutResult
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
The underlying RawCondvar implementation provided by C is permitted
to panic if requested to wait on two distinct MutexGuards simultaneously.
Sourcepub fn wait_for<T: ?Sized>(
&self,
mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>,
timeout: <C::RawMutex as RawMutexTimed>::Duration,
) -> WaitTimeoutResult
pub fn wait_for<T: ?Sized>( &self, mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>, timeout: <C::RawMutex as RawMutexTimed>::Duration, ) -> WaitTimeoutResult
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
The underlying RawCondvar implementation provided by C is permitted
to panic if requested to wait on two distinct MutexGuards simultaneously.
Sourcepub fn wait_while_until<T, F>(
&self,
mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>,
condition: F,
timeout: <C::RawMutex as RawMutexTimed>::Instant,
) -> WaitTimeoutResult
pub fn wait_while_until<T, F>( &self, mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>, condition: F, timeout: <C::RawMutex as RawMutexTimed>::Instant, ) -> WaitTimeoutResult
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 to true, 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 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
The underlying RawCondvar implementation provided by C is permitted
to panic if requested to wait on two distinct MutexGuards simultaneously.
Sourcepub fn wait_while_for<T: ?Sized, F>(
&self,
mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>,
condition: F,
timeout: <C::RawMutex as RawMutexTimed>::Duration,
) -> WaitTimeoutResult
pub fn wait_while_for<T: ?Sized, F>( &self, mutex_guard: &mut MutexGuard<'_, C::RawMutex, T>, condition: F, timeout: <C::RawMutex as RawMutexTimed>::Duration, ) -> WaitTimeoutResult
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 to true, 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 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
The underlying RawCondvar implementation provided by C is permitted
to panic if requested to wait on two distinct MutexGuards simultaneously.