Synchronization Shared Memory
<p>I have shared memory communication successful between two processes on Windows. I write to it like this:
<img src="http://i.stack.imgur.com/4nGNm.png" alt="enter image description here"></p>

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

<p>The way I do current do it is (Very SLOW!):</p>

<p>//Create SharedMemory Using Filename + KnownProcessID so I can have multiple clients and servers with unique mappings. I already have this working and communication successful.</p>

<pre><code>bool SharedMemoryBusy()
{
    double* Data = static_cast&lt;double*&gt;(pData);   //Pointer to the mapped memory.
    return static_cast&lt;int&gt;(Data[1]) &gt; 0 ? true : false;
}

void* RequestSharedMemory()
{
    for (int Success = 0; Success &lt; 50; ++Success)
    {
        if (SharedMemoryBusy())
            Sleep(10);
        else
            break;
    }
    return pData;
}

bool SharedMemoryReturned()
{
    double* Data = static_cast&lt;double*&gt;(pData);
    return static_cast&lt;int&gt;(Data[1]) == 2 ? true : false;
}

bool SharedDataFetched()
{
    for (int Success = 0; Success &lt; 50; ++Success)
    {
        if (SharedMemoryReturned())
            return true;

        if (!SharedMemoryBusy())
            return false;

        Sleep(10);
    }
    return false;
}
</code></pre>

<p>The reading and writing part (Just an example of on request.. There can be many different types.. Models, Points, Locations, etc):</p>

<pre><code>void* GLHGetModels()
{
    double* Data = static_cast&lt;double*&gt;(RequestSharedMemory());
    Data[0] = MODELS;
    Data[1] = StatusSent;

    if (SharedDataFetched())
    {
        if (static_cast&lt;int&gt;(Data[2]) != 0)
        {
            unsigned char* SerializedData  = reinterpret_cast&lt;unsigned char*&gt;(&amp;Data[3]);
            MemDeSerialize(ListOfModels, SerializedData, static_cast&lt;int&gt;(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&lt;unsigned char*&gt;(&amp;Data[3]);
        MemSerialize(Destination, ListOfModels); //Uses MEMCOPY To copy to Destination.
    }
}
</code></pre>
