- The lock keyword can be used to ensure that a block of code is run, and the parameter supplied to the lock keyword must be a reference-type-based object used to define the scope of the lock. The lock keyword is generally preferable to using the lock keyword using the Monitor class than using the monitor class directly, on the one hand because lock is more concise and on the other hand because the lock Ensures that the underlying monitor can be released even if the protected code throws an exception. This is done through the finally keyword, which executes the associated code block, regardless of whether an exception is thrown.
- Monitor : Similar to the lock and SyncLock keywords, monitors prevent multiple threads from executing code blocks at the same time. the Enter method allows one and only one thread to continue executing the subsequent statement, and all other threads will be blocked until the thread that executes the statement calls Exit. This is the same as using the Lock keyword
- Synchronization events: With notification capability
Having a thread wait for a non-terminating synchronization event can suspend the thread and change the event state to terminate to activate the thread. If a thread attempts to wait for an event that has already been terminated, the thread continues execution without delaying
Set method: Puts the time in the terminating state, activates the thread
There are two types of synchronization events:autoresetevent and manualresetevent. The only difference between them is that whenever AutoResetEvent activates a thread, its state automatically changes from terminating to non-terminating. Instead,ManualResetEvent allows its terminating state to activate any number of threads, reverting to a non-terminating state only when its Reset method is called.
- Mutex object (mutex)
Unlike monitors, named mutexes can be used to synchronize threads across processes, and only threads that have a mutex are freed
- Interlocked class
Provides atomic operations for variables shared by multiple threads.
- ReaderWriterLock class
Defines a lock that supports a single write thread and multiple read threads.
- Semaphore Signal Volume
Using a counter to access the shared resource, the semaphore constructor needs to provide the initialized counter (semaphore) size and the maximum counter size.
When accessing a shared resource, the program first applies for a license to Semaphore, Semaphore the license counter accordingly minus one, when the counter is 0 o'clock, other threads that request the semaphore license will be blocked, Until the thread that has previously applied to the license frees the license that he occupies, let the counter add one, so that the most recent thread that has applied for the license will get a license to be released for the competition.
The amount of initialization semaphore is the same as the current thread called WaitOne(Maximumcount minus initialcount).
Named semaphore system-level effective
C # thread synchronization