pub struct SharedMemoryObject<T> { /* private fields */ }
Expand description
An object that can be shared between processes.
After spawning child process (using fork()
, clone()
, etc.) updates to this object will be seen by both processes.
This is achieved by allocating memory using mmap with MAP_SHARED
flag.
For more details see man page.
§Example
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;
}
Implementations§
Sourcepub fn new(obj: T) -> Result<Self>
pub fn new(obj: T) -> Result<Self>
Allocates shared memory and moves obj
there.
§Errors
If allocation fails returns error from last_os_error
.
Trait Implementations§
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more