process-sync

Crates.ioprocess-sync
lib.rsprocess-sync
version0.2.2
sourcesrc
created_at2023-01-02 21:13:19.119882
updated_at2023-01-03 14:53:07.229811
descriptionSynchronization primitives for multiprocess environments
homepagehttps://github.com/Forestryks/process-sync-rs
repositoryhttps://github.com/Forestryks/process-sync-rs
max_upload_size
id749665
size24,305
Andrei Odintsov (Forestryks)

documentation

README

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()?;
}
Commit count: 12

cargo fmt