We help IT Professionals succeed at work.

What happens if a thread acquires a lock gets terminated before it releases the lock?

What happens if a thread acquires a lock (SemaphoreSlim), and then that thread gets terminated before it releases the lock?

e.g.
private SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
public async Task DoStuff()
{
    using (semaphoreSlim.WaitAsync(1000))
    {
    	...//Do something
    }
}

Open in new window

What happens if the thread gets terminated while it's in "Do Something"?
Comment
Watch Question

Owner (Aidellio)
Most Valuable Expert 2015
Commented:
You might get lucky with the garbage collector, however, you'll most likely end up with a memory leak.