POSIX shared memory synchronization in all C ++ / C ++ 11 processes

Problem (in short): I use POSIX Shared Memory and currently only use POSIX semaphores, and I need to manage multiple readers, multiple authors. I need help regarding which variables / methods I can use to control access within the limitations described below. I found an approach that I want to implement, but I do not know what methodology I can use to implement it when using POSIX shared memory.

What i found is https://stackoverflow.com/a/2126161/2326322 ... This link has an algorithm that I would like to use, but I'm not sure how to implement it with shared memory. Am I somehow storing a class in shared memory? Here I need help, please. The reason I'm not sure is because a lot of my research indicates that shared memory for primitives is only in order to avoid problems with problems, and STL objects cannot be used.

Note: For all my multi-threaded I use C ++ 11. This shared memory will be completely separate executable programs using C ++ 11 std :: threads, from which any thread of any process / executable will want to access. I have avoided pthread Linux for any of my multi-threaded operations and will continue to do so (except that only its control variable is not the actual pThreads).

Solution options designed to

  • Sharing should be available between 2+ processes that will run several C ++ 11 std :: thread, which they may wish to access. That is, Several writers (exclusive one at a time), while simultaneously providing several simultaneous readers when no author wants access.
  • Do not use BOOST libraries. Ideally native C ++ 11 or linux built-in libraries, something that will work without the need to install abstract libraries.
  • Do not use the actual pThread threads, but can use some kind of object from there that will work with C ++ 11 std :: thread.
  • It can ideally cope with a process malfunction during operation. For instance. Using the POSIX semaphore, if the process fires when it has a semaphore, everything is screwed. Have I seen people using file locks?

Thank you in advance

+1
source share
1 answer

saving shared memory for primitives only to avoid problems with problems

You can use pointers in shared memory objects in programs and for them, provided that the mmap ed memory matches the same address. This is actually a simple sentence, especially for 64 bits. See this open source C library that I wrote for implementation details: rszshm is a resizable shared memory with a pointer .

Using the POSIX semaphore, if a process crashes when it has a semaphore, everyone is screwed.

If you want to use mediated OS semaphores, SysV semaphores have SEM_UNDO, which is restored in this case. OTHH pthread offers robust mutexes that can be embedded and shared in shared memory. This can be used to create more complex mechanisms.

The SysV scheme for providing multiple semaphores in a semaphore set, where an action group must be successful, or call blocks, also allows you to create a complex mechanism. Read / write locks can be performed using a set of three semaphores.

+1
source

All Articles