Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 797
kralyk Avatar asked Mar 08 '26 18:03

kralyk


1 Answers

  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!
like image 78
Jean-Baptiste Yunès Avatar answered Mar 10 '26 06:03

Jean-Baptiste Yunès



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!