1

I have created a Boost Shared memory, for the purpose of sharing vectors.

The sharing has been accomplished.

However, I do not understand how the vectors are pushed into shared memory.

I do a push_back to the shared memory from the writing process. So the vectors are being pushed like a stack push into the shared memory, in LIFO order?

The other application, the reader, retrieves the vector in the following fashion :

managed_shared_nmemory segment (open_only, "Shared_mem_name");
Vector = segment.find<VECTOR_TYPE>("Vector_name").first;

if (Vector != NULL)
{
    //CODDE
}

Now here, which vector am I reading. the one pushed in last ( newest one )? And if I'm reading it, does it imply the vector is popped? i.e. is it still there in the shared memory after the read, and if so, will the shared memory overflow after a time, and how would I stop it? I don't see anything in the documentation regarding it...

user1173240
  • 1,455
  • 2
  • 23
  • 50
  • Check that you have proper process synchronization **and** are using the IPC allocator in `VECTOR_TYPE`. I suggest you start with a sample from the documentation. – sehe Feb 06 '14 at 13:00

1 Answers1

1

The vectors aren't "pushed" into the shared memory. There is no implicit stack.

Instead, the vector lives in the shared memory. What you're reading is in fact the very same vector as in another process, at the very same time.

Note that this strongly implies that you need a shared mutex to synchronize access too, otherwise you'll have data races between the threads (from different processes) and by definition this is Undefined Behaviour in C++.


On a slightly different angle:

I do a push_back to the shared memory from the writing process. So the vectors are being pushed like a stack push into the shared memory, in LIFO order?

Breaking it down:

  • I do a push_back to the shared memory

    No you don't. You push a vector element to the back of one vector. This vector is the single thing you shared, under the name "Vector_name"

  • So the vectors are being pushed

    No, your vectors stay where they are: in shared memory.

  • like a stack push into the shared memory, in LIFO order?

    No, there's only 1 vector. A vector is itself a ordered sequential container. It supports random access, so

    for (auto it = v.begin(); it!= v.end(); ++it)
    {  // process `*it` in order
    } 
    

    vs

    for (auto it = v.rbegin(); it!= v.rend(); ++it)
    {  // process `*it` in reversed order
    } 
    

    If you always use push_back to insert, then in order iteration is like "FIFO"

sehe
  • 374,641
  • 47
  • 450
  • 633
  • So, in this case, if I read a record `Vector = segment.find("Vector_name").first;` , I'm actually reading the very first one that was written on it? And what is the behaviour when the shared memory gets filled? Does it start overwriting from the first? – user1173240 Feb 07 '14 at 12:25
  • No. As always, you'll get `std::bad_alloc` exception if allocator runs out of memory? – sehe Feb 07 '14 at 14:03
  • 1
    @user1173240 PS. If you **want** this behaviour, look at boost::circular_buffer e.g. http://www.boost.org/doc/libs/1_55_0/doc/html/circular_buffer.html – sehe Feb 07 '14 at 14:04