Explain lock statement – C# Threading ?

lock ensures that another thread does not enter critical section if one thread is in critical section code. This means one thread gets mutual exclusive lock on the given object. Another thread is blocked i.e waits until the object is released.

lock has a simple syntax :

lock(referencetype variable)
{
    block of statements ; // critical section
}

Example below :
 lock (this)
      {
                     balance = balance – amount;
      }

Leave a Reply