Atomics in shared memory

⚓ Rust    📅 2025-07-07    👤 surdeus    👁️ 4      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Atomics in shared memory

Hello everyone!

Recently, I was experimenting with shared (memory-mapped) memory between processes, and synchronization between them. On Linux, there are POSIX semaphores that can be placed in shared memory and be used for synchronization, so I guess as long as all access is volatile and synchronized, everything is fine. I wonder about atomics in shared memory -- Rust standard library atomics have a from_ptr() unsafe constructors that take an aligned pointer and give an atomic object reference to perform operations -- however, I'm currently not sure whether it's okay (for example), for &AtomicInt32 to point to a shared memory. Standard library atomics are implemented with UnsafeCell, which allows the value to be mutated while there are shared references to the cell (but not mutable references to the cell or shared references to the value itself), however, I've read that Rust references are "non-volatile" (meaning the compiler assumes that the value cannot be accessed/modified "from the outside") -- does that apply to UnsafeCell references? If a value can be safely modified from other threads (provided that a modification uses atomic operations only), can similar modifications safely happen from other processes (or signal handlers)? Or is it better to use atomic intrinsics for shared memory instead?

10 posts - 5 participants

Read full topic

🏷️ rust_feed