kralyk
kralyk

Reputation: 4397

Questions about Boost shared memory

I'm trying to put up a simple but portable way to use shared memory. Boost::interprocess seemed like a good place to start, but I ran into some problems/concerns.

  1. Can I somehow query existence of shm segment, preferably using boost API? I could always try to create it using create_only and catch an exception, but that's bad design, I don't want stack unwinding in the "good" path.
  2. Can I truncate the segment even while other processes are attached? (provided I'll handle the synchronization, ofcourse) I suppose all the other processes would have to re-map, would they also have to re-attach?
  3. Boost doc says that on windows the portable shared_memory_object isn't actually shared memory per se, but rather a memory-mapped file. Did I understand that correctly? This means I'll have to use specialized code on windows, which I was trying to avoid. Makes me question Boost's fitness for my purpose, is there an alternative? Instead of fighting with boost, I might as well write platform-specific code myself - in your opinion, would that be worth the effort?

Upvotes: 3

Views: 921

Answers (1)

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36441

  1. no that is not bad design. It is a standard way to do so with IPCs. You (usually) don't have access to the naming system and have a kind of list of existing objects like a filesystem.
  2. If I remember well: if you truncate while mapped, there will be memory violations on new invalidated addresses (if memory protection is supported). You don't have to remap, you just have to take care about what you are doing. I'm not sure that you really need to truncate a SHM but that's your problem.
  3. that is not a problem, it just means that the underlying object is a file because standard semantic for SHM include persistence. But don't care, that is boost internals tricks. The semantic is the one you want, so use it to get portability!

Upvotes: 1

Related Questions