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.