3

I am implementing boost::circular_buffer in boost::managed_shared_memory. I used the sample code demonstrating vector in shared memory here. I made the following changes :

1) typedef boost::circular_buffer<int, ShmemAllocatorCB> MyCircularBuffer;

2) MyCircularBuffer *circbuff = segment.construct<MyCircularBuffer>("MyCB")(alloc_inst);

3) circbuff->push_back(1);

The code is giving a compile error on line #3. The error is

error C2665: 'operator new' : none of the 5 overloads could convert all the argument types c:\boost2005\boost\circular_buffer\base.hpp 1470

From the documentation, I understand the push_back function is expecting one of these 3 forms :

void push_back(param_value_type); void push_back(rvalue_type); void push_back();

I tried the empty parameter call, tried casting the 1 into param_value_type, rvalue_type but none of them seem to work. This might be a simple mistake but I have not been able to figure this out for quite some time now. Any help is appreciated. Thanks.

Edit:

typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocatorCB;

managed_shared_memory segment(create_only, "MySharedMemory", 65536);

const ShmemAllocatorCB alloc_inst (segment.get_segment_manager());

Maverick
  • 33
  • 6
  • Well, the problem seems to be to construct the object rather than pushing it. I suspect the error will persist if you comment in the push_back call. I think you need to elaborate more. In particular: how is the `ShmemAllocatorCB` defined and what is `alloc_inst`? – hfhc2 Jun 30 '14 at 08:37
  • @hfhc2 : Please check the edit. The error is introduced exactly at the push_back() call. If I comment it out the program compiles just fine. – Maverick Jul 01 '14 at 15:49

2 Answers2

2

It looks like Boost's circular buffer uses raw pointers at some point in time for debugging. This, unfortunately, is prohibitive:

Boost provides some reference on the subject:

In summary, compile your code without circular_buffer debugging support by defining either BOOST_CB_DISABLE_DEBUG or NDEBUG preprocessor defines.

EDIT: Here is a working example:

(tested with BOOST_LIB_VERSION = 1_49)

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/circular_buffer.hpp>
#include <iostream>
#include <string>
#include <cstdlib> //std::system

using namespace boost::interprocess;

//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocatorCB;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef boost::circular_buffer<int, ShmemAllocatorCB> MyCircularBuffer;

//Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char *argv[])
{
   if(argc == 1){ //Parent process
      //Remove shared memory on construction and destruction
      struct shm_remove
      {
         shm_remove() { shared_memory_object::remove("MySharedMemory"); }
         ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
      } remover;

      //Create a new segment with given name and size
      managed_shared_memory segment(create_only, "MySharedMemory", 65536);

      //Initialize shared memory STL-compatible allocator
      const ShmemAllocatorCB alloc_inst (segment.get_segment_manager());

      //Construct a buffer named "MyCB" in shared memory with argument 10 (for size) and alloc_inst
      MyCircularBuffer *circbuff = segment.construct<MyCircularBuffer>("MyCB")(10, alloc_inst);

      for(int i = 0; i < 100; ++i)  //Insert data in the buffer, with overflows
         circbuff->push_back(i);

      //Launch child process
      std::string s(argv[0]); s += " child ";
      if(0 != std::system(s.c_str()))
         return 1;

      //Check child has destroyed the buffer
      if(segment.find<MyCircularBuffer>("MyCB").first)
         return 1;
   }
   else{ //Child process
      //Open the managed segment
      managed_shared_memory segment(open_only, "MySharedMemory");

      //Find the buffer using the c-string name
      MyCircularBuffer *circbuff = segment.find<MyCircularBuffer>("MyCB").first;

      //Use buffer in reverse order
      std::cout << "Child got: " << (*circbuff)[3] << "\n"; // 93

      //When done, destroy the buffer from the segment
      segment.destroy<MyCircularBuffer>("MyCB");
   }

   return 0;
};

To complie it:

g++ -DBOOST_CB_DISABLE_DEBUG -DNDEBUG cb_ipc.cpp -o cb_ipc -lboost_system -lrt

To run it:

./cb_ipc
Child got: 93
Community
  • 1
  • 1
ftwl
  • 151
  • 1
  • 4
  • 3
    Beat me to it. There's also spsc_queue in Boost Lockfree that fits the purpose if there's only a single consumer/producer: http://stackoverflow.com/questions/22207546/shared-memory-ipc-synchronization-lock-free/22209595#22209595 – sehe Jun 30 '14 at 08:57
  • All these crazy ways of doing IPC! Absolutely mad! – ftwl Jun 30 '14 at 09:34
  • @ftwl : Thanks for the suggestion but I already tried that and it doesn't work. Although it is weird that if I change my build settings to Release instead of Debug, the error changes to `none of the 4 overloads could convert all the argument types` . – Maverick Jul 01 '14 at 15:43
  • @sehe: Thanks for the spsc queue suggestion, I will try that out. Although,could you please comment what is wrong with respect to the orignal question. I tried using the preprocessors but that didn't work. – Maverick Jul 01 '14 at 16:45
  • @Maverick the answer already gave the explanation, otherwise I would have posted the exact same answer. And yes, that made the code compile. – sehe Jul 01 '14 at 20:45
  • @ftwl:I am sorry I didnt mention before, I am using MSVC 2010 and boost 1_55. to compile this. I tried the code you posted but the error still persists. I have added the preprocessors in the project settings. Is there something else that I need to take care of in Visual Studio? – Maverick Jul 01 '14 at 21:31
  • @Maverick: I'm not sure... Maybe try adding the defines to the very top of the file: `#define BOOST_CB_DISABLE_DEBUG` `#define NDEBUG` – ftwl Jul 01 '14 at 22:58
  • @ftwl: That isn't working either. This thing is driving me crazy! Anyways, do let me know in case you figure it out at any point. Thanks for all your help! – Maverick Jul 02 '14 at 19:10
0

The issue here is that you are not setting the capacity of the circular_buffer correctly. I would imagine the capacity is zero and hence you cannot push.

After constructing the circular_buffer set the capacity to the required size.

#define BOOST_CB_DISABLE_DEBUG

#include <boost/circular_buffer.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

using namespace boost;
using namespace boost::interprocess;

typedef allocator<T, managed_shared_memory::segment_manager> cb_alloc;
typedef circular_buffer<T, cb_alloc> cb;

managed_shared_memory* m_mem_segment;                                               
cb* m_buffer;

//  Create shared memory segment of a size able to hold the max updates.
m_mem_segment = new managed_shared_memory(open_or_create, NAMED_MEMORY, MEMORY_SIZE);       

if (m_mem_segment == nullptr) {
    return false;
}
//  Get the segement manager for allocating a circular buffer.
const cb_alloc alloc_inst(m_mem_segment->get_segment_manager());                            

//  Create a circular buffer in the shared memory segment.
m_buffer = m_mem_segment->find_or_construct<cb>(NAMED_BUFFER)(alloc_inst);                  

if (m_mem_segment == nullptr) {
    return false;
}

//  Set the capacity if not set (zero) (this must be set!)
if (m_buffer->capacity() <= 0) {                                                    
    m_buffer->set_capacity(50);
}

You can push to this fine, I tested with multiple processes exchanging data fine. This is a simple example you obviously must implement your own synchronism between processes using named_mutex or semaphores etc.

MrKiwi
  • 343
  • 3
  • 10
Bumble
  • 21
  • 5