

The static object syncLock is used to implement a critical section in the GetServerLoadBalancerEngine function. ServerLoadBalancerEngine implements the Singleton pattern and here we need to ensure that multiple thread calls to GetServerLoadBalancerEngine should be synchronized. We can't achieved these by implementing a lock statement.

The Monitor class provides the Wait, Pulse, and PulseAll methods to implement timeout, signaling, and a consumer/producer design. There are certain conditions where a Monitor gives you the necessary control. Using this attribute, the Enter and Exit statements are not needed.Ī second option is a lock statement to implement a critical section. If a critical section spans an entire method, the locking facility described above can be achieved by placing .MethodImplAttribute on the method, and specifying the synchronized value in the constructor of MethodImplAttribute. Static methods are protected in a similar fashion using the type of the current instance as the locked object. Since only one thread can hold the lock on the current instance, the method can only be executed by one thread at a time. If an instance method requires synchronized thread access, it invokes the Enter and corresponding Exit methods using the current instance as the object to lock. This facility is typically used to synchronize access to a static or instance method of a class. In this case, it is recommended you place those instructions in a try block and place the Exit instruction in a finally block. If the critical section is a set of contiguous instructions, then the lock acquired by the Enter method guarantees that only a single thread can execute the enclosed code with the locked object.

Use the Enter and Exit methods to mark the beginning and end of a critical section. The Monitor.Enter() and Monitor.Exit() methods implement a critical section block. NET Framework, there are many primitives available that you can use to make synchronized calls to shared data. A class whose members are protected from such interruptions is called thread-safe. If code is not synchronized, then one thread might interrupt another thread and the object could be left in an invalid state. In multi-threaded applications where multiple threads make calls to the methods of a single object, it is necessary that those calls be synchronized.
