Monitor
This lesson explains how the Monitor class in C# works and its related caveats.
We'll cover the following
Monitor
The Monitor
class exposes static methods to synchronize access to objects. The following static methods are commonly used to work with the Monitor
class:
Enter()
Exit()
Wait()
Pulse()
PulseAll()
Synchronization on an object means that an exclusive lock is acquired on an object by a thread that invokes Enter()
. All other threads invoking Enter()
get blocked till the first thread exits the monitor by invoking Exit()
. As a consequence, the code block sandwiched between the enter and exit calls becomes a critical section with only one thread active in the block at any given time.
As an example consider the snippet below:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.