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§
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Creates new SharedCondvar
§Errors
If allocation or initialization fails returns error from last_os_error
.
Sourcepub fn wait(&mut self, mutex: &mut SharedMutex) -> Result<()>
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
.
Sourcepub fn notify_one(&mut self) -> Result<()>
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
.
Sourcepub fn notify_all(&mut self) -> Result<()>
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
.