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?
c # mutex memory-mapped-files
Mario
source share