Search code examples
c#multithreadingconcurrencylockingthread-synchronization

What happens to other threads when resource is locked in C#?


I wonder what does other threads do when one thread locked a resource. Can other threads do new tasks while they are waiting the lock to be released (like async programming) or they just doing nothing ?

C# code example:

lock (referenceType)
{
    // executing code
}

Solution

  • The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.

    This link is useful lock statement