lock


The lock keyword is used to protect resources when working in a multithreaded environment. The general form of the lock statement is:

lock (obj)
 {
   // critical section goes here
 }

where obj is the object to be locked. If another thread has entered the critical section, then the current thread will wait until the other thread exits the critical section. When the lock is granted, the critical section is executed.