tags:

views:

120

answers:

2

Can I use shared memory on Windows in order to have a common memory region that can be used by multiple separate processes?

Context:

Porting a unix application to Windows which has: - a 'setup' process that creates a number of shared memory regions. - a set of other processes (that run at times when the setup process has already finished) that read (and occasionally write) the memory regions that have been prepared by the setup process


I have already experimented with CreateFileMapping(INVALID_HANDLE_VALUE, ...)/OpenFileMapping but the shared memory seems to be freed as soon as the creating process exits.

Should I create real (i.e. on file system) files and open these instead of using INVALID_HANDLE_VALUE?

A: 

Boost has a library that can be used for shared memory: Boost.Interprocess

+2  A: 

Windows doesn't support creating a global file mapping object that exists without any processes holding handles to it, which explains why your use of CreateFileMapping with anonymous sections didn't work.

However, as you suggest you can create an actual file on disk, and map that using CreateFileMapping as needed, and processes can share memory that is backed by the disk file. Your processes will just need to know where to look to find the backing file.

Greg Hewgill