#60 Lock-C#

Lock is a thread safe mechanism,where you allow only one thread to access a  block of code(Critical Section) and block other threads,untill the current thread releases the lock(Mutual Exclusion).

lock (object)
 {
 // crtical section
 }

Internally the Lock get’s translated with Monitor.Enter(Object) and Monitor.Exit(object). Lock is a wrapper/high level construct provided by C#.

#56 Critical Sections-Threads

Critical Section: Critical Section is a piece of code or a shared resource that  must not be accessed concurrently by different threads.

Problems(If CS is not handled):

1.Race Condition.

2.DeadLock

Mechanism to handle Critical Section:

1.Atomic Updates

2.Partitioning

3.Wait-Based Synchronization Protocol.