pub struct SharedMutex { /* private fields */ }
Expand description

Simple mutex that can be shared between processes.

This mutex is NOT recursive, so it will deadlock on relock.

Dropping mutex in creating process while mutex being locked or waited is undefined behaviour. It is recommended to drop this mutex in creating process only after no other process has access to it.

For more information see pthread_mutex_init, pthread_mutex_lock and SharedMemoryObject.

Example

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

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

if pid == 0 {
    println!("child lock()");
    mutex.lock()?;
    println!("child locked");
    sleep(Duration::from_millis(40));
    println!("child unlock()");
    mutex.unlock()?;
} else {
    sleep(Duration::from_millis(20));
    println!("parent lock()");
    mutex.lock()?;
    println!("parent locked");
    sleep(Duration::from_millis(20));
    println!("parent unlock()");
    mutex.unlock()?;
}

Output:

child lock()
child locked
parent lock()
child unlock()
parent locked
parent unlock()

Implementations§

Creates new SharedMutex

Errors

If allocation or mutex initialization fails returns error from last_os_error.

Locks mutex.

This function will block until mutex is locked.

Unlocks mutex.

This function must be called from the same process that called lock previously.

Trait Implementations§

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

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