Experience in using thread classes in C #

Source: Internet
Author: User

1. Mutex class

MSDN simple description:

When two or more threads need to access a shared resource at the same time, the system needs to use a synchronous mechanism to ensure that only one thread uses the resource at a time.MutexIs a synchronization primitive, which only grants an exclusive access to shared resources to a thread. If a thread obtains the mutex, the second thread to obtain the mutex will be suspended until the first thread releases the mutex.

You can use the WaitHandle. WaitOne method to request the ownership of the mutex. A thread with a mutex can request the same mutex in the repeated call to WaitOne without blocking its execution. However, the thread must call the ReleaseMutex method for the same number of times to release the ownership of the mutex.MutexClass forced thread ID, so the mutex can only be released by the thread that obtains it. On the contrary, the Semaphore class does not force a thread ID.

There are two types of mutex: Local mutex and named system mutex. If you create a constructed function that accepts the nameMutexObject. The named system mutex is visible throughout the operating system and can be used to synchronize process activities. You can create multipleMutexObject To indicate the same named system mutex. You can also use the OpenExisting method to open the existing named system mutex.

The local mutex only exists in your process. Any reference part in your processMutexAll the threads of the object can use it. EachMutexThe object is a separate local mutex.

My understanding of Mutex:

1. Two threads cannot be used for interactive execution. If the Mutex object is placed in a loop of two threads, when the thread executes a critical section, both threads will immediately grab the Mutex object, therefore, a thread may be executed multiple times in a loop!

Example:

For (int I = 0; I <count; I ++)

{

Mutex. WaitOne ();

..........

Mutex. releaseMutex ();

} In a thread, this loop may be executed multiple times.

2. One of its greatest uses is described in the yellow text above, which can prevent a program from creating multiple processes.

Ii. Monitor (or Lock)

MSDN simple description:

MonitorClass controls access to objects by granting an object lock to a single thread. Object locks provide the ability to restrict access to code blocks (usually known as critical sections. When a thread has an object lock, no other thread can obtain the lock. You can also useMonitorTo ensure that no other thread is allowed to access the application code section being executed by the lock owner, unless another thread is using another lock object to execute the code.

MonitorHas the following features:

  • It is associated with an object as needed.
  • It is not bound, that is, it can be called directly from any context.
  • Cannot createMonitorClass.

The following information is maintained for each synchronization object:

  • Reference to the thread currently holding the lock.
  • References to the ready queue, which contains the thread for preparing to obtain the lock.
  • A reference to the waiting queue. It contains a thread waiting for notification of status changes of the locked object.

The following table describes the operations that a thread can take to access a synchronization object:

Operation

Description

Enter, TryEnter

Obtain the object lock. This operation also marks the beginning of the critical section. No other thread can enter the critical section unless it uses other locked objects to execute commands in the critical section.

Wait

Release the lock on the object to allow other threads to lock and access the object. When other threads access an object, the calling thread will wait. The pulse signal is used to notify the waiting thread of changes to the object state.

Pulse (signal), PulseAll

Sends signals to one or more waiting threads. This signal indicates that the status of the waiting thread lock object has changed, and the lock owner is ready to release the lock. Wait for the thread to be placed in the ready queue of the object so that it can finally receive the object lock. Once the thread has a lock, it can check the new State of the object to see if it has reached the desired state.

Exit

Release the lock on the object. This operation also marks the end of the critical section protected by the locked object.

UseEnterAndExitMethod To mark the beginning and end of a critical section. If the critical section is a continuous instruction setEnterThe lock obtained by the method ensures that only one thread can use the Lock Object to execute the Code contained.

My personal understanding of the Monitor class:

1. in fact, the description of MSDN seems that the Monitor class and Mutex class have similar functions. They both lock the critical section to achieve multi-thread synchronization. In fact, if you only use the Enter and Exit methods of the Monitor class, the Mutex class will also be used to lock the critical section, that is, in the critical section, each thread (including the thread that just released the lock) the lock will be snatched. However, with the Wait and Pluse methods of the Monitor class, the two threads can reach the mutex access to the critical zone, that is, each thread accesses the interactive access to its own thread context (which is better? Can I use my own code area ?), However, these two methods must be placed in the critical section locked with Lock or Monitor (using the Enter and Exit methods!

Related Article

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.