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 in the car, other passengers can get on the car only 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:

Code 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 (); // when the thread ends, set event4 to a signal state.
}
}
}

Program output result:

Result-Main owns GM1 and gm2
T1start started, mutex. waitall (mutex [])
T2start started, gm1.waitone ()
T3start started, mutex. waitany (mutex [])
T4start started, gm2.waitone ()
-Main releases GM1
T2start finished, gm1.waitone () satisfied
T3start finished, mutex. waitany (mutex [])
-Main releases gm2
T1start finished, mutex. waitall (mutex []) satisfied
T4start finished, gm2.waitone ()
Mutex sample

 

From the execution results, we can clearly see that the running of thread T2 and T3 is conditional on the release of GM1, and T4 is executed after the release of gm2, t1 is executed only after both GM1 and gm2 are released. At the end of the main () function, waithandle is used to wait for the signals of all autoresetevent objects. The signals of these objects represent the end of the corresponding thread.

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.