TimedLock
A Lock structure with timeout and stack traces in case of deadlock
Here's the original blog post that introduces the concept from Ian Griffiths.
I wrote a series of blog posts trying to improve on this.
- A lock statement with timeout
- A TimedLock success story
- TimedLock Revisited
- TimedLock Yet Again Revisited...
- TimedLock with stack traces strikes back
I finally moved the code into this Repository.
Usage Example
try
{
TimedLock timeLock = TimedLock.Lock(obj);
//Thread safe operations
timeLock.Dispose();
}
catch(LockTimeoutException e)
{
Console.WriteLine("Couldn't get a lock!");
StackTrace otherStack = e.GetBlockingThreadStackTrace(5000);
if(otherStack == null)
{
Console.WriteLine("Couldn't get other stack!");
}
else
{
Console.WriteLine("Stack trace of thread that owns lock!");
}
}
Note, you'll only ever get the other stack trace in DEBUG
builds.