

The class grants exclusive access to a shared resource for writing and allows multiple threads to access the resource simultaneously for reading. When SpinLock attempts to acquire a lock that is unavailable, it waits in a loop, repeatedly checking until the lock becomes available.įor more information about the benefits and drawbacks of using spin lock, see the SpinLock article and the SpinLock API reference. The structure, like Monitor, grants exclusive access to a shared resource based on the availability of a lock. You can also call the Mutex.OpenExisting method to open an existing named system mutex.įor more information, see the Mutexes article and the Mutex API reference.

To create a named mutex instance, use a Mutex constructor that specifies a name. To do that, use a named mutex, which is visible throughout the operating system. Unlike Monitor, the Mutex class can be used for inter-process synchronization. Like Monitor, Mutex has thread affinity and the thread that acquired a mutex must release it by calling the Mutex.ReleaseMutex method. Use one of the Mutex.WaitOne method overloads to request the ownership of a mutex. The class, like Monitor, grants exclusive access to a shared resource. Those statements are implemented by using the Enter and Exit methods and a try…finally block to ensure that the acquired lock is always released. Use the lock statement in C# and the SyncLock statement in Visual Basic to synchronize access to a shared resource instead of using the Monitor class directly. You can coordinate the interaction of threads that acquire a lock on the same object by using the Monitor.Wait, Monitor.Pulse, and Monitor.PulseAll methods.įor more information, see the Monitor API reference. Because the Monitor class has thread affinity, the thread that acquired a lock must release the lock by calling the Monitor.Exit method. You can also use the Monitor.TryEnter method to specify the amount of time during which a thread attempts to acquire a lock. The Enter method acquires a released lock. Any other thread is blocked from acquiring the lock and the Monitor.Enter method waits until the lock is released. While a lock is held, the thread that holds the lock can again acquire and release the lock. The class grants mutually exclusive access to a shared resource by acquiring or releasing a lock on the object that identifies the resource. NET provides a range of synchronization primitives to control access to a shared resource by multiple threads. Synchronization of access to a shared resource For example, SemaphoreSlim is a lightweight alternative to Semaphore. Some of those types are alternatives to the types derived from WaitHandle. Use those types for thread synchronization within one application. However, they cannot be used for the inter-process synchronization. Lightweight synchronization types don't rely on underlying operating system handles and typically provide better performance. NET 5+, some of these types can represent named system synchronization handles, which are visible throughout the operating system and can be used for the inter-process synchronization:įor more information, see the WaitHandle API reference. NET Framework, because WaitHandle derives from System.MarshalByRefObject, these types can be used to synchronize the activities of threads across application domain boundaries. , which derives from EventWaitHandle and, when signaled, stays in a signaled state until the Reset method is called.

, which derives from EventWaitHandle and, when signaled, resets automatically to an unsignaled state after releasing a single waiting thread., which represents a thread synchronization event and can be either in a signaled or unsignaled state.The state of a semaphore is set to signaled when its count is greater than zero, and nonsignaled when its count is zero. , which limits the number of threads that can access a shared resource or a pool of resources concurrently.The state of a mutex is signaled if no thread owns it. , which grants exclusive access to a shared resource.NET synchronization primitives derive from the class, which encapsulates a native operating system synchronization handle and uses a signaling mechanism for thread interaction. WaitHandle class and lightweight synchronization types If you use different synchronization primitive instances to protect the same resource, you'll circumvent the protection provided by a synchronization primitive. Use the same synchronization primitive instance to protect access of a shared resource.
