pub struct CondSync<T>(/* private fields */);Expand description
A thin wrapper around Arc<(Mutex<T>, Condvar)>.
It enhances readability when synchronizing threads
(compare with the examples given for Condvar).
§Example: Inform main thread when all child threads have initialized:
use cond_sync::{CondSync, Other};
use std::{thread, time::Duration};
// we use here a plain usize as condition state:
let cond_sync = CondSync::new(0_usize);
for i in 0..5 {
let cond_sync_t = cond_sync.clone();
thread::spawn(move || {
println!("Thread {i}: initializing ...");
// modify the state:
cond_sync_t.modify_and_notify(|v| *v += 1, Other::One).unwrap();
thread::sleep(Duration::from_millis(1)); // just to produce a yield
println!("Thread {i}: work on phase 1");
});
}
// [main thread] wait here until the condition is fulfilled:
cond_sync.wait_until(|v| *v == 5).unwrap();
println!("Main: All threads initialized");
thread::sleep(Duration::from_millis(100)); // just to let the threads finish (better use join)prints something like
Thread 0: initializing ...
Thread 2: initializing ...
Thread 1: initializing ...
Thread 3: initializing ...
Thread 4: initializing ...
Main: All threads initialized
Thread 2: work on phase 1
Thread 0: work on phase 1
Thread 1: work on phase 1
Thread 4: work on phase 1
Thread 3: work on phase 1Implementations§
Source§impl<T> CondSync<T>
impl<T> CondSync<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Construct a new instance, based on the variable you logically need to manage the synchronization.
Sourcepub fn wait_until<F>(&self, condition: F) -> Result<Reason, PoisonedError>
pub fn wait_until<F>(&self, condition: F) -> Result<Reason, PoisonedError>
Blocks the current thread until the given condition,
when called with the current value of the wrapped variable, returns true.
§Errors
This function will return an error if the internally used mutex being waited on is poisoned when this thread tries to re-acquire the lock. For more information, see information about poisoning on the Mutex type.
Sourcepub fn wait_until_or_timeout<F>(
&self,
condition: F,
duration: Duration,
) -> Result<Reason, PoisonedError>
pub fn wait_until_or_timeout<F>( &self, condition: F, duration: Duration, ) -> Result<Reason, PoisonedError>
Blocks the current thread until the given test method,
when called with the current value of the wrapped variable, returns true, but no longer
than the given duration.
§Returns
Returns true if the timeout was reached, and false if the condition was fulfilled.
§Errors
This function will return an error if the internally used mutex being waited on is poisoned when this thread re-acquires the lock. For more information, see information about poisoning on the Mutex type.
Sourcepub fn wait_timeout(&self, duration: Duration) -> Result<Reason, PoisonedError>
pub fn wait_timeout(&self, duration: Duration) -> Result<Reason, PoisonedError>
Blocks the current thread until a notification is received, but no longer than the given duration.
§Returns
Returns true if the timeout was reached, and false otherwise.
§Errors
This function will return an error if the internally used mutex being waited on is poisoned when this thread re-acquires the lock. For more information, see information about poisoning on the Mutex type.
Sourcepub fn modify_and_notify<F>(
&self,
modify: F,
other: Other,
) -> Result<(), PoisonedError>
pub fn modify_and_notify<F>( &self, modify: F, other: Other, ) -> Result<(), PoisonedError>
Applies a change to the wrapped variable (by calling the given function modify) and
notifies one or all of the other affected threads, depending on the value of other.
§Errors
This function will return an error if the internally used mutex being waited on is poisoned when this thread re-acquires the lock. For more information, see information about poisoning on the Mutex type.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for CondSync<T>
impl<T> RefUnwindSafe for CondSync<T>
impl<T> Send for CondSync<T>where
T: Send,
impl<T> Sync for CondSync<T>where
T: Send,
impl<T> Unpin for CondSync<T>
impl<T> UnwindSafe for CondSync<T>
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)