0

I wish to share a vector in shared memory, to enable it to be used across processes. However, I had not put in any synchronization, which was creating certain issues.

For creating this shared vector,I had followed the steps as given in the Boost Documentation, i.e., I had created a memory allocator for this vector, and had mapped the vector accordingly.

Now, however, I wish to have synchronization with the vector. Looking at the example for Boost IPC synchronization, they show a structure, having the semaphores, and the array which it would want to share, and then mapping this structure to the shared memory instead.

Now, if I have to adapt this to my existing code, I've to include my vector, in such a structure, and then map that structure in shared memory? So I've to have a different allocator, and do not need to use the existing vector allocator? Would this work?

This is related to the question : Undefined vector behaviour , which I had asked earlier.

EDIT

Some code : (in this piece of code, I'm sharing an array, instead of the vector, as the use would be just fine. I've put the mutex in with the structure, and then tried to share the structure in memory, but it does not work. Gives unhandled access violation errors when trying to get the mutex to wait.)

srvCheck.h

typedef struct
{
    TCHAR  name[100];
    DWORD someVariable;

} SERVER_INFO_TYPE;

struct sync_semaphore
{
    sync_semaphore() :
        mutex(1), nEmpty(0), nStored(0)
    {}

    boost::interprocess::interprocess_semaphore mutex, nEmpty, nStored;

    SERVER_INFO_TPYE arrayName[64];
    int varName;
}

class someClass
{
    public : 
        someclass()
            someFunc();
    private:
        sync_semaphore * data;
        void * addr;
}

srvCheck.cpp

someClass::someClass() //constructor of someClass
{
    shared_memory_object shm
        (create_only                 
        ,"MySharedMemory"          
        ,read_write
        );

    shm.truncate(sizeof(sync_semaphore));


    mapped_region region
        (shm                       //What to map
        ,read_write //Map it as read-write
        );

    addr   = region.get_address();

    data = new (addr) sync_semaphore;
}

someClass::someFunc()
{
    <some code>
    data->mutex.wait(); // unhandled exception - I tried putting the constructor stuff in the same function, but the error persists.
    <some code>
}

I have added in some code, and have tried to put the question in a better order. Kindly advise.

Community
  • 1
  • 1
user1173240
  • 1,455
  • 2
  • 23
  • 50
  • There's no clear answer. The answer in general is "yes", "yes", "yes", and "yes". The exception being that you don't need to wrap anything in a struct to be able to put it into shared memory – sehe Apr 03 '14 at 08:09
  • If I elect not to have the vector in the `struct`, how would I be able to associate the semaphores with it? Won't it be shared separately, in a separate section of the `shared memory`? Also, following the example, `mutex->wait()` throws unhandled exception, access violation... – user1173240 Apr 03 '14 at 08:23
  • You don't need a semaphore, per se. Any synchronization object will do (depending on goals, maybe). You can use a named_* one. Indeed, if you use an anonymous sync object, then putting it with the data might be nice, but not a tangible difference. It's your choice. As long as you put it in shared memory, obviously – sehe Apr 03 '14 at 08:25
  • _"Also, following the example"_ - there's no code. If you are stuck getting the sample to work, post **that** as you concrete question. With SSCCE – sehe Apr 03 '14 at 08:27
  • Added some code, kindly advise. – user1173240 Apr 03 '14 at 08:57
  • That looks like C code, not C++. It also looks like it couldn't ever compiler (Linterprocess?). Also, you omitted shared_memory_buffer. – sehe Apr 03 '14 at 09:11
  • I've corrected and put in some code. – user1173240 Apr 04 '14 at 03:29

0 Answers0