Mutex of thread synchronization mode, mutex of Thread Synchronization

Source: Internet
Author: User

Mutex of thread synchronization mode, mutex of Thread Synchronization

The mutex volume is very similar to that in the critical section. Only threads with mutex objects can access Shared resources. Only one mutex object can be accessed, therefore, we can ensure that only one thread can access Shared resources at the same time to achieve thread synchronization.

The mutex is more advanced than the critical section. You can name the mutex and support cross-process thread synchronization. Mutex is the operation of the Win32 API called on the mutex lock. Therefore, different processes in the same operating system can share the lock according to the name of the mutex lock.

Because of this, mutex lock operations will improve resources, and the performance will be lower than that of the critical section, so you need to consider it more. The performance of using the critical zone for intra-process thread synchronization is better.

In. Net, the Mutex class is used to represent the Mutex volume. The instance WaitOne () is used to prevent the thread from executing the Mutex object and ReleaseMutex () to release the lock resource. Let's take a look at the example below.

Class MutexTest {// create the Mutex object private static Mutex mutex = new Mutex (true, "My"); // static void Main (string [] args) {Run (); console. readLine () ;}// simulate multiple threads to call public static void Run () {const int count = 2; var threads = new Thread [count]; for (var I = 0; I <count; I ++) {var ts = new ThreadStart (UserResource); var t = new Thread (ts); t. name = "thread" + (I + 1); threads [I] = t;} foreach (var t in threads) {t. start ();} // Console. writeLine (string. format ("Creating thread {0} owns the Mutex. ", Thread. currentThread. name); Thread. sleep (1000); mutex. releaseMutex (); Console. writeLine (string. format ("Creating thread {0} releases the Mutex. ", Thread. currentThread. name);} // modify the shared resource value public static void UserResource () {mutex. waitOne (); Console. writeLine (string. format ("Child thread named {0} owns the mutex. ", Thread. currentThread. name); Thread. sleep (1000); mutex. releaseMutex (); Console. writeLine (string. format ("Child thread named {0} releases the mutex. ", Thread. currentThread. name ));}}

A Mutex object is a global object. You can specify the name when constructing a Mutex object. If the name is the same, it is considered to be the same Mutext object. In addition, the first Bool parameter specified during the reconstruction indicates whether the thread that creates the Mutex object has the Mutex.

If it is True, it indicates yes. It must be released before it can be used by other threads. In the above code example, the main thread needs to release the Mutex.

First, create a Mutex object from the main thread and have the Mutex object

The main thread then executes the command and creates two threads. The thread uses WaitOne to stop the thread from continuing to execute until it receives the signal released by the Mutex object to obtain the right to use the Mutex and continue the execution.

1 second after Sleep, the main thread releases the Mutex object

The sub-thread WaitOne obtains the release signal and obtains the Mutex ownership. The sub-thread enters the resource code execution, and the other sub-thread will not be able to obtain the Mutex ownership until ReleaseMutex.

When Mutex is used, WaitOne is used to prevent thread execution until the Mutex object ownership is obtained. You can also specify a blocking time. If the Mutex ownership fails to be obtained upon timeout, the subsequent code will continue to be executed. After the execution is completed, the Mutex does not need to be released.

WaitOne should be used in pairs with ReleaseMutex. Remember to release the Lock Object after obtaining the Lock Object; otherwise, other threads will remain in the waiting state.

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.