Putting a mutex in a memory mapped file in windows in C / C ++

Does Windows offer some kind of mutex that can be put into a memory mapped file and used by multiple processes?

Ideally, it should be completely self-contained so that it can survive on its own in the file even on reboot. Also, no resources should be missed if I just delete the file manually while the processes are not running.

If possible, the solution should also suggest an accompanying concept of "condition", which should also be an object that can reside in a shared memory mapped file.

In short, I need something similar to a PTHREADS mutex with a SHARED attribute.

It is my understanding that simply using Mutex PTHREADS is not possible because the SHARED attribute is not supported on the Windows PTHREADS port.

+3


source to share


3 answers


To share a sync object, specify a name for it and use the same name in each process when you create the object.

The following synchronization objects can be shared between processes in this way:



Critical sections cannot be shared, but faster.

Testing or waiting for these objects is done using a family of waiting functions, often WaitForMultipleObjects .

+2


source


Use this file as your own mutex: use a function LockFileEx

and agree that everyone locks byte 0 of the file when they want to claim the mutex.



+2


source


It's impossible. The mutex object itself lives in kernel space to protect it from getting user code into state. The pen you purchased is only valid for the process that purchased it. Technically, you can use DuplicateHandle () and put the returned handle in mmf, but only if you have a handle to another process that is accessing the memory section. It's pretty fragile.

This is why you can specify a name for the mutex in the CreateMutex () function. Another process navigates to it using the same name in the call to OpenMutex.

+1


source







All Articles