Struct sync_wait_object::WaitEvent
source · pub struct WaitEvent<T>(_);
Expand description
Create a wait event object of any type T. To use this wait object in multi-threaded scenario, just clone the object and distribute it.
This wait object is just a wrapper of Mutex and Condvar combination with the suggested pattern (from Rust document) for waiting a value.
There are two ways to wait. The first is just to want until an expected value.
use std::thread;
let wait3 = WaitEvent::new_init(0);
let mut wait_handle = wait3.clone();
thread::spawn(move || {
for i in 1..=3 {
wait_handle.set_state(i).unwrap();
}
});
let timeout = std::time::Duration::from_secs(1);
let r#final = *wait3.wait(Some(timeout), |i| *i == 3).unwrap();
let current = *wait3.value().unwrap();
assert_eq!(r#final, 3);
assert_eq!(current, 3);
The second is to wait and then reset the value to a desired state.
use std::thread;
let wait3 = WaitEvent::new_init(0);
let mut wait_handle = wait3.clone();
thread::spawn(move || {
for i in 1..=3 {
wait_handle.set_state(i).unwrap();
}
});
let timeout = std::time::Duration::from_secs(1);
let r#final = wait3.wait_reset(Some(timeout), || 1, |i| *i == 3).unwrap();
let current = *wait3.value().unwrap();
assert_eq!(r#final, 3);
assert_eq!(current, 1);
Implementations§
source§impl<T> WaitEvent<T>
impl<T> WaitEvent<T>
pub fn new_init(initial_state: T) -> Self
pub fn value(&self) -> Result<MutexGuard<'_, T>>
sourcepub fn wait(
&self,
timeout: Option<Duration>,
checker: impl FnMut(&T) -> bool
) -> Result<MutexGuard<'_, T>>
pub fn wait(
&self,
timeout: Option<Duration>,
checker: impl FnMut(&T) -> bool
) -> Result<MutexGuard<'_, T>>
Wait until the checker
returns true, or timed-out from timeout
.
Arguments
timeout
- Maximum wait timechecker
- Checker function, once it returnstrue
, the wait ends
sourcepub fn wait_reset(
&self,
timeout: Option<Duration>,
reset: impl FnMut() -> T,
checker: impl FnMut(&T) -> bool
) -> Result<T>
pub fn wait_reset(
&self,
timeout: Option<Duration>,
reset: impl FnMut() -> T,
checker: impl FnMut(&T) -> bool
) -> Result<T>
Wait until the checker
returns true, or timed-out from timeout
. If the wait ends from checker
condition, the interval value is reset by reset
.
Arguments
timeout
- Maximum wait timereset
- Function that provides a reset valuechecker
- Checker function, once it returnstrue
, the wait ends
Examples
use sync_wait_object::{WaitEvent, WaitObjectError};
let wait3 = WaitEvent::new_init(0);
let mut wait_handle = wait3.clone();
thread::spawn(move || {
for i in 1..=3 {
thread::sleep(Duration::from_millis(50));
wait_handle.set_state(i).unwrap();
}
});
let timeout = Duration::from_millis(250);
let r#final = wait3.wait_reset(Some(timeout), || 0, |i| *i == 5);
let current = *wait3.value().unwrap();
assert_eq!(r#final, Err(WaitObjectError::Timeout));
assert_eq!(current, 3);
pub fn wait_with_waiter(
&self,
timeout: Option<Duration>,
checker: impl FnMut(&T) -> bool
) -> Result<MutexGuard<'_, T>>
pub fn wait_and_reset_with_waiter(
&self,
timeout: Option<Duration>,
checker: impl FnMut(&T) -> bool,
reset: impl FnMut() -> T
) -> Result<T>
pub fn set_state(&mut self, new_state: T) -> Result<()>
Trait Implementations§
source§impl From<AutoResetEvent> for WaitEvent<bool>
impl From<AutoResetEvent> for WaitEvent<bool>
source§fn from(value: AutoResetEvent) -> Self
fn from(value: AutoResetEvent) -> Self
Converts to this type from the input type.
source§impl From<ManualResetEvent> for WaitEvent<bool>
impl From<ManualResetEvent> for WaitEvent<bool>
source§fn from(value: ManualResetEvent) -> Self
fn from(value: ManualResetEvent) -> Self
Converts to this type from the input type.