Struct SharedCondvar

Source
pub struct SharedCondvar { /* private fields */ }
Expand description

Simple conditional variable that can be shared between processes and used with SharedMutex

Dropping conditional variable in creating process while it being used by another process will cause undefined behaviour. It is recommended to drop this conditional variable in creating process only after no other process has access to it.

For more information see pthread_cond_init, pthread_cond_wait, SharedMutex and SharedMemoryObject.

§Example

let mut mutex = SharedMutex::new()?;
let mut condvar = SharedCondvar::new()?;

let pid = unsafe { fork() };
assert!(pid >= 0);

if pid == 0 {
    println!("child lock()");
    mutex.lock()?;
    println!("child wait()");
    condvar.wait(&mut mutex)?;
    println!("child notified");
    mutex.unlock()?;
    println!("child unlocked");
} else {
    sleep(Duration::from_millis(40));
    println!("parent notify()");
    condvar.notify_one()?;
}

Output:

child lock()
child wait()
parent notify()
child notified
child unlocked

Implementations§

Source§

impl SharedCondvar

Source

pub fn new() -> Result<Self>

Creates new SharedCondvar

§Errors

If allocation or initialization fails returns error from last_os_error.

Source

pub fn wait(&mut self, mutex: &mut SharedMutex) -> Result<()>

Waits on given mutex

This function will block until notified by another process

§Errors

If any pthread call fails, returns error from last_os_error. For possible errors see pthread_cond_wait.

Source

pub fn notify_one(&mut self) -> Result<()>

Notifies one of processes that are waiting on this condvar

§Errors

If any pthread call fails, returns error from last_os_error. For possible errors see pthread_cond_signal.

Source

pub fn notify_all(&mut self) -> Result<()>

Notifies all processes that are waiting on this condvar

§Errors

If any pthread call fails, returns error from last_os_error. For possible errors see pthread_cond_broadcast.

Trait Implementations§

Source§

impl Drop for SharedCondvar

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.