views:

264

answers:

1

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:

  1. 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?
  2. 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.
  3. 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.

+1  A: 

The ring buffer is not a problem here, using shared memory is. The solution to that is using placement new for allocating your object, or at least for allocation of an internal buffer of it. Simply having member functions, constructors and destructors shouldn't cause trouble, but make sure that you do not use virtual functions or contain non-POD objects, as that may get tricky.

Tronic
Virtual functions won't work at all, and there is no way to make them work.
Joseph Garvin