简体   繁体   中英

c++ - using shared memory with multiple processes

I'm using Boost, specifically shared_memory_object and mapped_region to implement basic IPC (shared memory).

Now, I wanna extend the program, so the shared memory could be access from several separated process for not only reading but also for changing the data.

The first thing I thought about was synchronization between these processes. Actually the requirement is to lock the shared memory when any of those processes is willing to access the object, but as the resource is defined out of the scope of those programs .. how can it be done ?!

Could you please first tell me if this requirement already implemented into those classes implicitly? and if not could please elaborate how to achieve this goal?

Could you please first tell me if this requirement already implemented into those classes implicitly ?

No, there is no built-in synchronization in shared memory because on memory level it is not clear which groups of operations on it must be atomic. But there are synchronization primitives in boost::interprocess which you can use.

and if not could please elaborate how to achieve this goal?

You need a mutex . Your problem looks like readers-writer problem, so upgradable_mutex can be a better solution because it supports shareable read lock and exclusive write lock. But you may find other synchronization primitives from boost::interprocess useful too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM