3

Simply I am trying to create a circular buffer in shared memory and perform some insert and search operations in it.

So first I create my types:

typedef allocator<MyData, managed_shared_memory::segment_manager> ShmemAllocator;
typedef boost::circular_buffer<MyData,ShmemAllocator> cbMyDataContainerType;

Pointers to segment and container:

managed_shared_memory *segment;
cbMyDataContainerType *cbMyDataContainer;

I have created a shared memory succesfully before and I'm using it to share some other objects already, so here I find the previously created shared memory and try to create the Circular buffer:

segment = new managed_shared_memory(open_only, "MySharedMemory");

const ShmemAllocator alloc_inst (segment->get_segment_manager());   

cbMyDataContainer = segment->construct<cbMyDataContainerType>
    ("MyDataContainerCircularBuffer")(alloc_inst), (CONTAINER_SIZE);

And try to insert some values here:

cbMyDataContainer->push_back(MyData(id, dataTime));

After that I try to dump to container like that:

int count = 0
for (cbMyDataContainerType::iterator it = cbMyDataContainer->begin(), 
     it_end = cbMyDataContainer->end(); it != it_end; ++it, count++) 
{       
    std::cout << "Id : "<< it->id << " Time : " << it->dTime <<  std::endl;
}

But the problem is; while creating the container it seems that everything is ok (at least no exceptions) but it does not take the size (sizeof(cbMyContainer) seems 8 but CONTAINER_SIZE is 10000 so it should be smth like 8 * 10000 ?) and I can not insert any item on the container.

When I try to dump the container it does not even enter the for loop since there is no inserted item in it.

Do you have any idea about what is wrong here?

Thanks indeed...

Note: I am using Visual Studio 2008 VC++, Win7 x64

Vecihi
  • 261
  • 4
  • 12
  • Strongly related recently answered: http://stackoverflow.com/q/22207546/85371 - no time to look at your code now. Can you make it a SSCCE? – sehe Mar 17 '14 at 14:50
  • It looks similar but i couldn't see what's wrong with my code? it is using a different container but does nothing extra i think, am i wrong? – Vecihi Mar 17 '14 at 15:03
  • 1
    By the way i have to use boost 1.47.0 so i think lockfree is not available for that version – Vecihi Mar 17 '14 at 15:22

1 Answers1

1

It been long time, but I think you should pass a size to circular buffer when construct it. Something like:

cbMyDataContainer = segment->construct<cbMyDataContainerType>
   ("MyDataContainerCircularBuffer")(CONTAINER_SIZE, alloc_inst);
Raven
  • 345
  • 2
  • 10
  • Note that `circular buffer` does not free `MyData` class instance. i.e. it does not call it destructor – oak Apr 22 '19 at 06:04
  • yes, but we can implement the assign operator to make the destructor get called. according to https://www.boost.org/doc/libs/1_70_0/doc/html/circular_buffer/implementation.html#circular_buffer.implementation.overwrite_operation – Raven Apr 23 '19 at 03:28
  • interesting, any idea how do I do in practice? – oak Apr 23 '19 at 06:22
  • @Reven this solution is similar to the use of `smart pointer,` isn't it? – oak Apr 23 '19 at 06:34