4 releases

0.2.2 Jan 3, 2023
0.2.1 Jan 3, 2023
0.2.0 Jan 3, 2023
0.1.0 Jan 2, 2023

#609 in Unix APIs

Download history 74/week @ 2024-11-20 207/week @ 2024-11-27 142/week @ 2024-12-04 182/week @ 2024-12-11 38/week @ 2024-12-18 110/week @ 2025-01-01 223/week @ 2025-01-08 41/week @ 2025-01-15 99/week @ 2025-01-22 121/week @ 2025-01-29 13/week @ 2025-02-05 37/week @ 2025-02-12 32/week @ 2025-02-19 59/week @ 2025-02-26 5/week @ 2025-03-05

133 downloads per month

MIT/Apache

18KB
182 lines

Process Sync

Crates.io Documentation

Adds synchronization primitives that can be shared between multiple processes

Shared memory

let mut shared = SharedMemoryObject::new(123)?;

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

if pid == 0 {
    assert_eq!(*shared.get(), 123);
    *shared.get_mut() = 456;
    sleep(Duration::from_millis(40));
    assert_eq!(*shared.get(), 789);
} else {
    sleep(Duration::from_millis(20));
    assert_eq!(*shared.get(), 456);
    *shared.get_mut() = 789;
}

Mutex

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()?;
}

Condvar

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()?;
}

Dependencies

~44KB