Article View: borland.public.cppbuilder.nativeapi
Article #127Re: Getting Started with Memory Mapped Files
From: Remy Lebeau (TeamB)
Date: Wed, 11 Jun 2008 20:24
Date: Wed, 11 Jun 2008 20:24
84 lines
3801 bytes
3801 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message
news:485028a2@newsgroups.borland.com...
> Firstly, in my application, my service writes events to an event log
> but at the same time, it also adds them to a stl vector. My service
> will be the ONLY process that should be able to write to the MMF.
> Is it still necessary to protect access with a mutex?
Yes, for the reason I already explained. If you don't protect access, the
the secondary process could read the MMF mid-write and the data it receives
would be wrong.
> I'm not clear here how the mutex protects anything...
> I assum that the logic would go something like this:
Not quite.
> // To write to the MMF
> 1) Try to open Mutex_1.
> 2) if Mutex_1 cannot be openned (i.e. nothing is using the MMF), create
> Mutex_1.
It is best to create the mutex ahead of time, such as when the MMF is
created/opened, and just lock/unlock the mutex when writing.
> if Mutex_1 can be openned (i.e. something is using the MMF), do what? Wait
> on it using WaitForSingleObject?
Yes. For example:
--- service ---
hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
0, 256, "MyFileMapping");
hMutex = CreateMutex(NULL, FALSE, "MyFileMappingMutex");
...
// for each write operation...
if( WaitForSingleObject(hMutex, 1000) == WAIT_OBJECT_0 )
{
// Perform write operation on the MMF...
ReleaseMutex(hMutex);
}
...
CloseHandle(hMutex);
CloseHandle(hMapping);
--- application ---
hMapping = OpenFileMapping(FILE_MAP_READ, FALSE, "MyFileMapping");
hMutex = OpenMutex(SYNCHRONIZE, FALSE, "MyFileMappingMutex");
...
// for each read operation...
if( WaitForSingleObject(hMutex, 1000) == WAIT_OBJECT_0 )
{
// Perform read operation on the MMF...
ReleaseMutex(hMutex);
}
...
CloseHandle(hMutex);
CloseHandle(hMapping);
> would the same be try for reading.
Similarly, yes. The secondary process can open the mutex when opening the
MMF, and then lock/unlock the mutex when reading.
> surely there is some overhead in creating the mutex each time.
Yes, which is why you shouldn't be creating it on each write. In fact,
doing so will provide no protection for the MMF at all.
> Also, in the case of (6) what action do you take if the mutex exists?
> If you just wait on it with WaitForSingleObject, surely there is the
> potential to hang the service if another process creates the mutex
> and keeps it open?
Potentially, yes. The WaitFor...() functions have a Timeout parameter you
can use to address that, though.
Gambit
Technical Details
Message-ID:
<485034aa$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<48500053@newsgroups.borland.com> <48501f3a$1@newsgroups.borland.com> <485028a2@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b80765c4c39946b4 X-Google-Attributes: gid10cc67,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <48500053@newsgroups.borland.com> <48501f3a$1@newsgroups.borland.com> <485028a2@newsgroups.borland.com> Subject: Re: Getting Started with Memory Mapped Files Date: Wed, 11 Jun 2008 13:24:10 -0700 Lines: 86 Organization: Lebeau Software X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-RFC2646: Format=Flowed; Response NNTP-Posting-Host: 207.178.157.90 Message-ID: <485034aa$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1213215914 207.178.157.90 (11 Jun 2008 13:25:14 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail