C # thread synchronization

Source: Internet
Author: User

  1. 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.
  2. 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
  3. 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.
  4. Mutex object (mutex)
    Unlike monitors, named mutexes can be used to synchronize threads across processes, and only threads that have a mutex are freed
  5. Interlocked class
    Provides atomic operations for variables shared by multiple threads.
  6. ReaderWriterLock class
    Defines a lock that supports a single write thread and multiple read threads.
  7. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.