Use of the C # multi-threaded series Mutex

Source: Internet
Author: User

Use of the C # multi-threaded series Mutex
The sample code using System; using System is copied. threading; class Example {// Create a new Mutex. the creating thread does not own the mutex. private static Mutex mut = new Mutex (); private const int numIterations = 1; private const int numThreads = 3; static void Main () {// Create the threads that will use the protected resource. for (int I = 0; I <numThreads; I ++) {Thread newThread = new Thread (new T HreadStart (ThreadProc); newThread. name = String. format ("Thread {0}", I + 1); newThread. start ();} // The main thread exits, but the application continues to // run until all foreground threads have exited .} private static void ThreadProc () {for (int I = 0; I <numIterations; I ++) {UseResource ();}} // This method represents a resource that must be synchronized // so that only one thread at a ti Me can enter. private static void UseResource () {// Wait until it is safe to enter, and do not enter if the request times out. console. writeLine ("{0} is requesting the mutex", Thread. currentThread. name); if (mut. waitOne (1000) {Console. writeLine ("{0} has entered the protected area", Thread. currentThread. name); // Place code to access non-reentrant resources here. // Simulate some work. thread. sl Eep (5000); Console. writeLine ("{0} is leaving the protected area", Thread. currentThread. name); // Release the Mutex. mut. releaseMutex (); Console. writeLine ("{0} has released the mutex", Thread. currentThread. name);} else {Console. writeLine ("{0} will not acquire the mutex", Thread. currentThread. name) ;}}// The example displays output like the following: // Thread1 is requesting the mutex // Thread 1 has entered the protected area // Thread2 is requesting the mutex // Thread3 is requesting the mutex // Thread2 will not acquire the mutex // Thread3 will not acquire the mutex // Thread1 is leaving the protected area // Thread1 has released the mutex copy code when a thread occupies Mutex, the code can be written as follows: mutex. waitOne (); mutex. waitOne (); mutex. waitOne (); mutex. waitOne ();.... When the WaitOne method is called, The system checks that the mutex method is not used by any thread. Therefore, the mutex method is allocated to the current thread. Therefore, even the call to WaitOne is not affected, because the system directly returns the message if the current thread is occupied.. In other words, waitOne is used to obtain the Mutex lock. If ReleaseMutex is called, the current thread exits the Mutex lock and other threads can apply for it. However, if you call ReleaseMutex twice, an exception will be thrown because the current thread does not actually occupy the lock. So it cannot be written like this: mutex. releaseMutex (); // if it already exists, OKmutex. releaseMutex (); // call the second direct exception Mutex is a kernel object, so it can be used for cross-process thread synchronization. A can write Mutex mutex = new Mutex (true, "mutex1"); B can write Mutex mutex = Mutex. openExisting ("mutex1") OpenExisting is used to obtain an existing kernel object. The thread synchronization code obtained is consistent with the documentary process. Complete.

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.