I have shared memory communication successful between two processes on Windows. I write to it like this:

Don’t mind my comments to myself. I thought I’d have some fun with the diagram.
How can I do this faster? In-other words, how can I do this using a Mutex or CreateEvent? I tried hard to understand Mutexes and CreateEvent but it confuses me on MSDN as it uses on application and a thread. An example would be helpful but isn’t required.
The way I do current do it is (Very SLOW!):
//Create SharedMemory Using Filename + KnownProcessID so I can have multiple clients and servers with unique mappings. I already have this working and communication successful.
bool SharedMemoryBusy()
{
double* Data = static_cast<double*>(pData); //Pointer to the mapped memory.
return static_cast<int>(Data[1]) > 0 ? true : false;
}
void* RequestSharedMemory()
{
for (int Success = 0; Success < 50; ++Success)
{
if (SharedMemoryBusy())
Sleep(10);
else
break;
}
return pData;
}
bool SharedMemoryReturned()
{
double* Data = static_cast<double*>(pData);
return static_cast<int>(Data[1]) == 2 ? true : false;
}
bool SharedDataFetched()
{
for (int Success = 0; Success < 50; ++Success)
{
if (SharedMemoryReturned())
return true;
if (!SharedMemoryBusy())
return false;
Sleep(10);
}
return false;
}
The reading and writing part (Just an example of on request.. There can be many different types.. Models, Points, Locations, etc):
void* GLHGetModels()
{
double* Data = static_cast<double*>(RequestSharedMemory());
Data[0] = MODELS;
Data[1] = StatusSent;
if (SharedDataFetched())
{
if (static_cast<int>(Data[2]) != 0)
{
unsigned char* SerializedData = reinterpret_cast<unsigned char*>(&Data[3]);
MemDeSerialize(ListOfModels, SerializedData, static_cast<int>(Data[2]));
return Models.data();
}
return NULL;
}
return NULL;
}
void WriteModels()
{
If (pData[0] == MODELS)
{
//Write The Request..
Data[1] = StatusReturned;
Data[2] = ListOfModels.size();
unsigned char* Destination = reinterpret_cast<unsigned char*>(&Data[3]);
MemSerialize(Destination, ListOfModels); //Uses MEMCOPY To copy to Destination.
}
}
You can use events and mutexes between processes. This works fine. You only need to duplicate the handle of the object before passing it to other process:
Each process works with its own handle while they both refer to the same kernel object.
What you need to do is to create 2 events. First party will signal that the data is ready. Other side shoud take the data, place its own data, reset first event and signal the second. After that the first party is doing the same.
Event functions are:
CreateEvent,SetEvent, …CloseHandle.