开发者

How can I find out which thread has the lock on a Mutex?

开发者 https://www.devze.com 2023-01-23 07:30 出处:网络
I use the .Net Mutex class to lock parts of my app across threads. I\'m having some deadlock issues, and it would be very helpful if I could find out the name of the thread that c开发者_Go百科urrently

I use the .Net Mutex class to lock parts of my app across threads. I'm having some deadlock issues, and it would be very helpful if I could find out the name of the thread that c开发者_Go百科urrently has the lock.

Is there an easy way to do that?


You can't, Mutex is a wrapper around a native Windows handle. Windows obfuscate handle values to prevent anybody from peeking at the internal kernel structures.

Add instrumentation to your code. Easy enough to store the value of the thread's ManagedId or Name every time you acquire the mutex. Logging is often useful to troubleshoot threading problems, although it is dangerous since it affects timing of the threads so much. The VS2010 Ultimate edition has a slick addon named Concurrency Visualizer.


You could create your own lock class based on Mutux. Obviously you would want to keep it as binary compatible as possible so that you can easily swap the existing references in code with the new class. The WaitOne and ReleaseMutex methods essentially just call into the real Mutex, but add logging or whatever else might be helpful for debugging.

0

精彩评论

暂无评论...
验证码 换一张
取 消