_c# tutorial on the usage analysis of Mutex objects in C #

Source: Internet
Author: User
Tags mutex sleep

The example in this article describes the usage of Mutex objects in C #. Share to everyone for your reference, specific as follows:

The C # language has a lot to learn, and here we'll introduce C # mutex objects, including aspects such as how to control the relationships between multiple threads.

How to control the relationship between multiple threads, without conflict and repetition, requires the use of mutually exclusive objects, namely: The Mutex class in the System.Threading namespace.

We can think of a mutex as a taxi and a passenger as a thread. Passengers first wait for the bus, then get on the bus and finally get off. When a passenger is in the car, the other passengers can only get on when he gets off the bus. This is also true of the thread's relationship to the C # mutex object, where the thread uses the Mutex.waitone () method to wait for the C # mutex object to be freed and automatically owns the C # mutex object if it waits to be freed. Until it calls the Mutex.releasemutex () method to dispose of the object, and during this time other threads that want to get the C # Mutex object will have to wait.

The following example uses a C # mutex object to synchronize four threads, while the main thread waits for the end of four threads, and the four threads are run in connection with two C # mutex objects.

It also uses the object of the AutoResetEvent class, which can be interpreted as a semaphore. Here the signal state is used to indicate the end of a thread.

Using System;
Using System.Threading; Namespace Threadexample {public class Mutexsample {static mutex gM1; static mutex gM2; const int iters =; static Aut
Oresetevent Event1 = new AutoResetEvent (false);
static AutoResetEvent Event2 = new AutoResetEvent (false);
static AutoResetEvent Event3 = new AutoResetEvent (false);
static AutoResetEvent Event4 = new AutoResetEvent (false);
public static void Main (string[] args) {Console.WriteLine ("Mutex Sample");
Creates a mutex object and is named Mymutex gM1 = new Mutex (true, "Mymutex");
Creates a mutex object that is not named.
gM2 = new Mutex (true);
Console.WriteLine ("-Main owns GM1 and gM2");
Autoresetevent[] EVs = new AUTORESETEVENT[4]; Evs[0] = Event1;
Defines the AutoResetEvent object evs[1 for the following thread t1,t2,t3,t4] = Event2;
EVS[2] = Event3;
EVS[3] = Event4;
Mutexsample TM = new Mutexsample ();
thread T1 = new Thread (new ThreadStart (Tm.t1start));
Thread t2 = new Thread (new ThreadStart (Tm.t2start));
thread t3 = new Thread (new ThreadStart (Tm.t3start)); thread T4 = new Thread (The new ThreadStart (tm).T4start)); T1. Start ()///Use the Mutex.waitall () method to wait for an object in a mutex array to be freed T2. Start ();//Use the Mutex.waitone () method to wait for gM1 release T3. Start ();//Use the Mutex.waitany () method to wait for any object in a mutex array to be freed T4.
Start ();//Use Mutex.waitone () method to wait for GM2 release Thread.Sleep (2000);
Console.WriteLine ("-Main releases gM1"); Gm1.releasemutex ();
Thread T2,t3 End condition satisfies thread.sleep (1000);
Console.WriteLine ("-Main releases gM2"); Gm2.releasemutex ();
Thread T1,t4 End condition satisfies//waits for all four threads to end WaitHandle.WaitAll (EVs);
Console.WriteLine ("Mutex Sample");
Console.ReadLine ();
public void T1start () {Console.WriteLine ("T1start started, Mutex.waitall (mutex[))");
mutex[] GMs = new mutex[2];
Gms[0] = gm1;//Create a mutex array as the parameter of the Mutex.waitall () method gms[1] = gM2;
Mutex.waitall (gMs);/Waiting for gM1 and gM2 to be released Thread.Sleep (2000);
Console.WriteLine ("T1start Finished, Mutex.waitall (mutex[)) satisfied"); Event1.set (); Thread end, set Event1 to signaled state} public void T2start () {Console.WriteLine ("T2start started, Gm1.waitone ()"); Gm1.waitone ()//etc Pending release of GM1 Console.WriteLine ("T2start Finished, gm1.waitOne () satisfied "); Event2.set ();//end of thread, set Event2 to signaled state} public void T3start () {Console.WriteLine ("T3start started, Mutex.waitany (mutex[
])");
mutex[] GMs = new mutex[2];
Gms[0] = gm1;//Create a mutex array as the parameter of the Mutex.waitany () method gms[1] = gM2;
Mutex.waitany (gMs);//wait for any one of the Mutex objects in the array to be freed Console.WriteLine ("T3start Finished, Mutex.waitany (mutex[)"); Event3.set ();//thread end, set Event3 to signaled state} public void T4start () {Console.WriteLine ("T4start started, Gm2.waitone ()"); gM2.
WaitOne ()//waiting for GM2 to be released Console.WriteLine ("T4start Finished, Gm2.waitone ()");

 Event4.set ()//thread end, set Event4 to signaled status}}

Read more about C # Interested readers can view the site topics: "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # programming Thread Usage Skill Summary"

I hope this article will help you with C # programming.

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.