Wednesday, February 23, 2011

Static Mutex (don’t do it)

I came across some code that was using a static mutex on a class was not static. When you call
 
mutex.WaitOne();

The second time it is called it will continue even if you have a not released the mutex with 

mutex.ReleaseMutex();


Lots of example use a static mutex and most are fine for what they are doing, but if you copy and paste the code you may find that it does not work for you and you only find out in production as how many people write tests for this? (personal… I do write concurrency tests for some cases, but… I don't think the unit test police would mind if you didn't)


If you need to use the mutex to sync code within your process don't use a static mutex.

No comments:

Post a Comment