2

Is there a way (other than a try-catch) to determine whether a boost::interprocess:managed_shared_memory region with a particular name already exists?

I know if I allocate an Interprocess vector within the managed_shared_memory region I can check it's existence using managed_shared_memory::find(), but there doesn't appear to be a way to check whether the managed_shared_memory region itself exists.

user997112
  • 29,025
  • 43
  • 182
  • 361
  • 3
    Does this answer your question? [Is there a better way to check for the existence of a boost shared memory segment?](https://stackoverflow.com/questions/25574401/is-there-a-better-way-to-check-for-the-existence-of-a-boost-shared-memory-segmen) – pooya13 Mar 01 '21 at 06:10

1 Answers1

1

You can use the constructor with boost::interprocess::open_or_create.

If you want to know which of the two happened, you can use boost::interprocess::open_only or boost::interprocess::create_only but you'll have add "external" synchronization on top: c++ Synchronize shared memory when reading

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Isn't that effectively suggesting using a try-catch? Try creating using create_only, if it fails you know the memory didn't exist? – user997112 May 01 '18 at 12:44
  • No, that's pointing out the different approaches you can use. You do not have to. In fact, you haven't told us why you think you have to. – sehe May 01 '18 at 14:02
  • 1
    Oh, by the way, if you know the platform details, you can, of course, use that knowledge (look for the file nodes under `/dev/shm/` e.g.). But I would suggest "abusing" exception handling over abusing implementation details that make the code non-portable – sehe May 01 '18 at 14:04