Why is creating a ring buffer shared by different processes so hard (in C++), what I am doing wrong?
- by recipriversexclusion
I am being especially dense about this but it seems I'm missing an important, basic point or something, since what I want to do should be common:
I need to create a fixed-size ring buffer object from a manager process (Process M). This object has write() and read() methods to write/read from the buffer. The read/write methods will be called by independent processes (Process R and W)
I have implemented the buffer, SharedBuffer<T&>, it allocates buffer slots in SHM using boost::interprocess and works perfectly within a single process. I have read the answers to this question and that one on SO, as well as asked my own, but I'm still in the dark about how to have different processes access methods from a common object. The Boost doc has an example of creating a vector in SHM, which is very similar to what I want, but I want to instantiate my own class.
My current options are:
Use placement new, as suggested by Charles B. to my question; however, he cautions that it's not a good idea to put non-POD objects in SHM. But my class needs the read/write methods, how can I handle those?
Add an allocator to my class definition, e.g. have SharedBuffer<T&, Alloc> and proceed similarly to the vector example given in boost. This sounds really complicated.
Change SharedBuffer to a POD class, i.e. get rid of all the methods. But then how to synchronize reading and writing between processes?
What am I missing? Fixed-length ring buffers are very common, so either this problem has a solution or else I'm doing something wrong.