Mutex "safe" processing?

I constantly read from a memory mapping file that another process writes and uses a mutex to synchronize this operation. In my few tests, while this works fine, but ... what if my application crashes immediately after acquiring the mutex and before its release? Is there a way to guarantee the release of the mutex, even in the event of such a failure?

Also, how can I handle the failure of another process that may not have released the mutex yet? Do I need to handle an AbandonedMutex exception every time I call mutex.WaitOne ()?

Now I am doing it something like this:

public MyState GetState() { MyState state = new State(); this._mutex.WaitOne(); try { state.X = this._mmView.ReadSingle(0); state.Y = this._mmView.ReadSingle(4); [..] } finally { this._mutex.ReleaseMutex(); } return state; } 

_mmView is a previously created instance of MemoryMappedViewAccessor. This entire GetState () method receives each frame as part of the game loop, approximately every few milliseconds.

PS: Also, is there another obvious problem, why could this end in failure, which I have not mentioned?

+8
c # mutex memory-mapped-files
source share
2 answers

Eugen's answer is correct - the operating system will release the mutex for you. Now think about the consequences of this fact:

  • You pulled out a mutex to make sure that no one would mutate a state while reading or read a state while you mutate. Assume the latter.
  • Another application wants to read the state, so it is trying to accept a mutex. He is forced to wait.
  • You mutated a condition and then crashed.
  • The operating system has released a mutex.
  • Another application now immediately receives the mutex, and now effectively reads the state, "and another process mutates it . The fact that the other process is now dead and gone means that the fictitious state will last forever, and the reading process will probably crash itself and dying horribly, you just defeated the security system provided by the mutex .

In short, you are worried about what is wrong. You do not have to worry about what happens if my mutex is ever released . The worst thing that happens then is that everyone waits forever, which is sad, but ultimately the user will restart the machine. You have to worry about what happens if mutex does get released, because I crashed halfway through the mutation. In this case, processes are now likely to crash everywhere and user data will be permanently corrupted.

Do not get into this situation first. The solution to the problem did not fail when you took out the mutex for recording . If you do not write programs that fail, you do not have to worry about it, because this will not happen. So just don't write programs that have ever crashed.

+11
source share

When the process ends when using the mutex, the OS will automatically release the mutex for you. Try this by finding the mutex and then raise new WhateverException() - another process will continue.

The same is true for all AFAIK synchronization primitives.

+2
source share

All Articles