C # multi-thread Learning (6) series of mutually exclusive objects) -- continue to search engine research

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 classProgramExample:

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 (); // Wait for the release of GM1 using the mutex. waitone () method
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 (); // Thread T2 and T3 finish conditions are met

Thread. Sleep ( 1000 );
Console. writeline ( " -Main releases gm2 " );
Gm2.releasemutex (); // Thread T1 and T4 end conditions 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 until any mutex object in the array is 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:

Mutex sample
  - 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.

 

 

C # multi-thread Learning Series (strongly recommended)

 

§ 1. C # multithreading (I) Related Concepts of multithreading (reprinted series) -- continue to search engine research

Http://www.cnblogs.com/OceanChen/archive/2009/02/02/1382233.html

2.C # multi-thread Learning (2) how to manipulate a thread (reprinted series) -- continue to search engine research

Http://www.cnblogs.com/OceanChen/archive/2009/02/02/1382232.html

§ 3.C # multi-thread Learning (iii) producer and consumer (reprinted series)-continue to search engine research

Http://www.cnblogs.com/OceanChen/archive/2009/02/02/1382231.html

§ 4.C # multi-thread Learning (iv) automatic management of multiple threads (thread pool) (reprinted series) -- continue to search engine research

Http://www.cnblogs.com/OceanChen/archive/2009/02/02/1382230.html

§ 5.C # multi-thread Learning (5) automatic management of multiple threads (timer) (reprinted series) -- continue to search engine research

Http://www.cnblogs.com/OceanChen/archive/2009/02/02/1382229.html

§ 6.C # multi-thread Learning (6) mutually exclusive objects (reprinted series) -- continue to search engine research

Http://www.cnblogs.com/OceanChen/archive/2009/02/02/1382227.html

ArticleSource: http://www.cnblogs.com/xugang/archive/2008/04/06/1138856.html

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.