This crate provides a simple interface to shared memory OS APIs.
Shared memory is well suited for sharing large amounts of data between processes as it relies purely on memory accesses. Other than when managing concurent access through locks/events, reading and writing memory from a SharedMem relies only on CPU features (the operating system is not involved, no context switches like system calls, etc...).
Writer based on examples/create.rs ``` rust //Creates a new SharedMem link "sharedmem.link" that points to shared memory of size 4096 let mut myshmem: SharedMem = match SharedMem::create( PathBuf::from("shared_mem.link"), LockType::Mutex, //Concurent accesses will be managed by a mutex 4096 ).unwrap();
//Acquire write lock
{
let mut shareddata: WriteLockGuardSlice
Reader based on examples/open.rs
rust
// Open an existing SharedMem link named "shared_mem.link"
let mut my_shmem: SharedMem = match SharedMem::open(PathBuf::from("shared_mem.link")).unwrap();
//Aquire Read lock
{
let mut shared_data = match my_shmem.rlock_as_slice::<u8>().unwrap();
//Print the content of the shared memory as chars
for byte in &shared_data[0..256] {
if *byte == 0 { break; }
print!("{}", *byte as char);
}
}
| Feature| Description | Linux | Windows| Mac[1]| |--------|-------------|:-----:|:------:|:----:| |SharedMem.create/open|Create/open a SharedMem|✔|✔|X| |SharedMem.*_raw|Create/open a raw shared memory map|✔|✔|X| |LockType::Mutex|Mutually exclusive lock|✔|✔|X| |LockType::RwLock|Exlusive write/shared read|✔|X[2]|X|
[1] I do not own a Mac so cannot implement that side of things myself. Contributions are welcome !
[2] Windows provides no default implementation of Rwlock that is safe to share between processes. See Issue #1
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.