CondSync

Struct CondSync 

Source
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 1

Implementations§

Source§

impl<T> CondSync<T>

Source

pub fn new(value: T) -> Self

Construct a new instance, based on the variable you logically need to manage the synchronization.

Source

pub fn wait_until<F>(&self, condition: F) -> Result<Reason, PoisonedError>
where F: Fn(&T) -> bool,

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.

Source

pub fn wait_until_or_timeout<F>( &self, condition: F, duration: Duration, ) -> Result<Reason, PoisonedError>
where F: Fn(&T) -> bool,

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.

Source

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.

Source

pub fn modify_and_notify<F>( &self, modify: F, other: Other, ) -> Result<(), PoisonedError>
where F: Fn(&mut T),

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.

Source§

impl<T> CondSync<T>
where T: Clone,

Source

pub fn clone_inner(&self) -> T

Produces a detached clone of the contained variable.

Trait Implementations§

Source§

impl<T> Clone for CondSync<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.