Provide an abstraction over Condvar
+ Mutex
usage, as provided by the Rust document
in Condvar.
The library provides three main types: WaitEvent
, ManualResetEvent
, and AutoResetEvent
. WaitEvent
is the core
abstraction mentioned. ManualResetEvent
and AutoResetEvent
are just a specialization for bool
type.
When compiling with Windows platform, the lib also provides windows
module for native implementation of
ManualResetEvent
and AutoResetEvent
.
Example of the abstraction provided:
```rust use syncwaitobject::WaitEvent; use std::thread;
let wait3 = WaitEvent::newinit(0); let mut waithandle = wait3.clone();
thread::spawn(move || { for i in 1..=3 { waithandle.setstate(i).unwrap(); } });
let timeout = std::time::Duration::fromsecs(1); let r#final = *wait3.wait(Some(timeout), |i| *i == 3).unwrap(); let current = *wait3.value().unwrap(); asserteq!(r#final, 3); assert_eq!(current, 3); ```
The second is to wait and then reset the value to a desired state. ```rust use syncwaitobject::WaitEvent; use std::thread;
let wait3 = WaitEvent::newinit(0); let mut waithandle = wait3.clone();
thread::spawn(move || { for i in 1..=3 { waithandle.setstate(i).unwrap(); } });
let timeout = std::time::Duration::fromsecs(1); let r#final = wait3.waitreset(Some(timeout), || 1, |i| *i == 3).unwrap(); let current = *wait3.value().unwrap(); asserteq!(r#final, 3); asserteq!(current, 1); ```