Question

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...

Was it helpful?

Solution

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"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top