Re: Shared memory delay

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Carl Daniel [VC++ MVP] (cpdaniel_remove_this_and_nospam_at_mvps.org.nospam)
Date: 01/16/05


Date: Sun, 16 Jan 2005 10:02:28 -0800

Tim Haynes wrote:
> Hello,
>
> An odd problem...
>
> I have shared memory between two processes using CreateFileMapping
> (using the system pagefile) and MapViewOfFile, and this seems to work
> fine. Then I added some signalling between the processes using a
> semaphore, so that one process puts data in the shared memory and
> raises the semaphore (ReleaseSemaphore) - then the thread in the
> other process (waiting on the semaphore in WaitForSingleObject wakes
> up and reads the data from shared memory.
>
> Sounds great, except...there seems to be a bit of a delay for the
> updated memory to make its way from one process to the other. The
> semaphore wakes the second process up almost instantly, but when it
> reads the shared memory, the data isn't there. If I pause the second
> process after it wakes from the semaphore but before reading the
> value, then all is fine. Even a Sleep(0) seems to make it work, but
> I was wondering why the delay? Is Sleep(0) the right way to fix it?
>
> Note I tried SEC_NOCACHE in CreateFileMapping, but no difference.

As others have replied, there is NO shared memory delay - it simply can't
happen.

Your problem sounds to me like a synchronization problem. e.g. a
producer/consumer queue where the consumer is synchronized to the producer
but is lagging behind by one message.

Keep in mind that the shared memory is (most likely) at a different base
address in the two processes - make sure you're accounting for that
correctly everywhere you're computing addresses into the shared region.

-cd



Relevant Pages

  • Re: Shared memory delay
    ... Thanks to everyone for the reassurance that there is no delay. ... > Tim Haynes wrote: ... >> I have shared memory between two processes using CreateFileMapping ... >> other process (waiting on the semaphore in WaitForSingleObject wakes ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Inter-processor atomic operationss
    ... difference between the implementation of this hypothetical synchronization ... Need to either always use a keyed event for the wait semaphore ... Don't refer to any memory not in the shared memory region. ... Does the Interlocked API set guarantee inter CPU atomic operations ...
    (microsoft.public.win32.programmer.kernel)
  • Inter Process Communication
    ... Two or several processes can exchange data through this resource. ... The shared memory ID is given as program argument. ... sem_create: creates a semaphore set. ...
    (comp.unix.programmer)
  • Re: Suggestions of design (contd.)
    ... The SNMP subagent creates a shared memory region and semaphore ... If a snmpget request comes then it needs to answer the desired ... but the safe functions are the minority out ...
    (comp.unix.programmer)
  • Shared memory delay
    ... I have shared memory between two processes using CreateFileMapping (using ... added some signalling between the processes using a semaphore, ... The semaphore wakes ... Note I tried SEC_NOCACHE in CreateFileMapping, ...
    (microsoft.public.win32.programmer.kernel)