4

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.

Jonas
  • 121,568
  • 97
  • 310
  • 388
recipriversexclusion
  • 13,448
  • 6
  • 34
  • 45
  • I'll add #4 - write a server process to manage the data. The basic rule about multi-process software is that something (i.e. one process) has to be in charge of a shared resource. –  Mar 11 '10 at 20:40
  • Exactly. That was what I was aiming with the management process that will create the buffer, check how full it is, etc. – recipriversexclusion Mar 11 '10 at 20:44
  • 1
    But if you have such a process, all issues of placement new, use of shared memory etc. go away. You of course have new interesting issues like how to talk to the server, but those are well understood in the biz. –  Mar 11 '10 at 20:51
  • Agree with @Neil, additionally you can pick up unexpected side-benefits too, like being able to spread your clients around to multiple machines. – Clinton Pierce Mar 11 '10 at 20:57
  • Not an answer to your question, but have you considered using a pipe and letting the OS take care of the synchronization for you? – dmckee --- ex-moderator kitten Mar 11 '10 at 21:06
  • The ring buffer is where a video decoder (writer) will place decoded video frames for processors (readers) to read, so speed is important. I didn't go the pipes way since I though SHM would buy me much more speed. – recipriversexclusion Mar 11 '10 at 21:11

1 Answers1

1

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
  • 10,250
  • 2
  • 41
  • 53