/TimedLock

A Lock structure with timeout and stack traces in case of deadlock

Primary LanguageC#MIT LicenseMIT

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.

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.