C # multi-thread Learning (6) mutually exclusive objects

Source: Internet
Author: User

The Mutex class in the System. Threading namespace is used to control the connections between multiple threads without conflict or repetition.
We can regard Mutex as a taxi and passengers as a thread. The passenger first waits for the car, then gets on the bus, and finally gets off the bus. When a passenger is on the bus, other passengers can only get on the bus after they get off the bus. This is also the relationship between the thread and the Mutex object. The thread uses Mutex. the WaitOne () method waits for the Mutex object to be released. If the Mutex object it waits for is released, it automatically owns the object until it calls Mutex. the ReleaseMutex () method releases this object. During this period, other threads that want to obtain this Mutex object only have to wait.
The following example uses a Mutex object to synchronize four threads. The main thread waits for the end of the four threads, and the running of these four threads is associated with two Mutex objects.
The AutoResetEvent class object is also used, which can be understood as a signal lamp. Here, its signal state is used to indicate the end of a thread.
// Set AutoResetEvent. Set () to a signal state.
// The AutoResetEvent. Reset () method sets it to stateless.
Mutex program example:

Using System;
Using System. Threading;

Namespace ThreadExample
{
Public class MutexSample
{
Static Mutex gM1;
Static Mutex gM2;
Const int ITERS = 100;
Static AutoResetEvent 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 ");
// Create a Mutex object and name it MyMutex
GM1 = new Mutex (true, "MyMutex ");
// Create an unnamed Mutex object.
GM2 = new Mutex (true );
Console. WriteLine ("-Main Owns gM1 and gM2 ");

AutoResetEvent [] evs = new AutoResetEvent [4];
Evs [0] = Event1; // defines the AutoResetEvent object for the subsequent threads t1, t2, t3, and t4.
Evs [1] = 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 (new ThreadStart (tm. t4Start ));
T1.Start (); // use the Mutex. WaitAll () method to wait for all objects in a Mutex array to be released
T2.Start (); // use the Mutex. WaitOne () method to wait for the release of gM1
T3.Start (); // use the Mutex. WaitAny () method to wait for any object in a Mutex array to be released
T4.Start (); // use the Mutex. WaitOne () method to wait for the release of gM2

Thread. Sleep (2000 );
Console. WriteLine ("-Main releases gM1 ");
GM1.ReleaseMutex (); // The end condition of thread t2 and t3 is met.

Thread. Sleep (1000 );
Console. WriteLine ("-Main releases gM2 ");
GM2.ReleaseMutex (); // The end conditions of thread t1 and t4 are met.

// Wait until all four threads 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 a parameter of the Mutex. WaitAll () method.
GMs [1] = gM2;
Mutex. WaitAll (gMs); // wait until both gM1 and gM2 are released
Thread. Sleep (2000 );
Console. WriteLine ("t1Start finished, Mutex. WaitAll (Mutex []) satisfied ");
Event1.Set (); // when the thread ends, set Event1 to a signal state.
}
Public void t2Start ()
{
Console. WriteLine ("t2Start started, gM1.WaitOne ()");
GM1.WaitOne (); // wait for the release of gM1
Console. WriteLine ("t2Start finished, gM1.WaitOne () satisfied ");
Event2.Set (); // when the thread ends, set Event2 to a signal state.
}
Public void t3Start ()
{
Console. WriteLine ("t3Start started, Mutex. WaitAny (Mutex [])");
Mutex [] gMs = new Mutex [2];
GMs [0] = gM1; // create a Mutex array as a parameter of the Mutex. WaitAny () method.
GMs [1] = gM2;
Mutex. WaitAny (gMs); // wait for any Mutex object in the array to be released
Console. WriteLine ("t3Start finished, Mutex. WaitAny (Mutex [])");
Event3.Set (); // when the thread ends, set Event3 to a signal state.
}
Public void t4Start ()
{
Console. WriteLine ("t4Start started, gM2.WaitOne ()");
GM2.WaitOne (); // wait for gM2 to be released
Console. WriteLine ("t4Start finished, gM2.WaitOne ()");
Event4.Set (); // end the thread and set Event4

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.