Lock Vs Monitor
Lock keyword in C# | Thread Locking In C#
The lock
keyword in C# is used to place around a critical section of code, where we want to allow only one thread to access the resource at a time. Any other thread cannot access the lock and it waits for the lock to be released.
In this post series, we will go through the understanding of the lock
keyword, monitor
, mutex
, and semaphore
available in C#.
All of these classes (lock, monitor, mutex, and semaphore) provide a synchronization mechanism to protect the shared code or resources in a multithreaded application.