Usage Analysis of Mutex Object in C

  • 2021-10-13 08:27:07
  • OfStack

This article illustrates the use of Mutex objects in C #. Share it for your reference, as follows:

The C # language has a lot to learn. Here we will introduce C # Mutex objects, including how to control the connection between multiple threads.

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

We can think of Mutex as a taxi and passengers as threads. Passengers wait for the bus first, then get on and get off at last. When one passenger is on the bus, other passengers can only get on the bus after he gets off the bus. The same is true of the thread's relationship with the C # Mutex object. The thread uses the Mutex. WaitOne () method to wait for the C # Mutex object to be released. If the C # Mutex object it is waiting for is released, it automatically owns the object until it calls the Mutex. ReleaseMutex () method to release the object, while other threads that want to get the C # Mutex object have to wait.

The following example uses the C # Mutex object to synchronize four threads, with the main thread waiting for the end of the four threads, which in turn are associated with two C # Mutex objects.

The object of AutoResetEvent class is also used, which can be understood as a signal lamp. Here, its signaled state is used to indicate the end of 1 thread.

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 1 A Mutex Object and named MyMutex
gM1 = new Mutex(true,"MyMutex");
// Create 1 Unnamed Mutex  Object .
gM2 = new Mutex(true);
Console.WriteLine(" - Main Owns gM1 and gM2");
AutoResetEvent[] evs = new AutoResetEvent[4];
evs[0] = Event1; // For the following thread t1,t2,t3,t4 Definition AutoResetEvent Object 
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 Mutex.WaitAll() Method wait 1 A Mutex All the objects in the array are released 
t2.Start( );//  Use Mutex.WaitOne() Method wait gM1 Release of 
t3.Start( );//  Use Mutex.WaitAny() Method wait 1 A Mutex Any in the array 1 Objects are released 
t4.Start( );//  Use Mutex.WaitOne() Method wait gM2 Release of 
Console.WriteLine(" - Main releases gM1");
gM1.ReleaseMutex( ); // Thread t2,t3 End condition satisfied 
Console.WriteLine(" - Main releases gM2");
gM2.ReleaseMutex( ); // Thread t1,t4 End condition satisfied 
// Waiting for all 4 End of threads 
Console.WriteLine(" Mutex Sample");
public void t1Start( )
Console.WriteLine("t1Start started, Mutex.WaitAll(Mutex[])");
Mutex[] gMs = new Mutex[2];
gMs[0] = gM1;// Create 1 A Mutex Array as Mutex.WaitAll() Parameters of the method 
gMs[1] = gM2;
Mutex.WaitAll(gMs);// Wait gM1 And gM2 Are all released 
Console.WriteLine("t1Start finished, Mutex.WaitAll(Mutex[]) satisfied");
Event1.Set( ); // When the thread ends, the Event1 Set to signaled state 
public void t2Start( )
Console.WriteLine("t2Start started, gM1.WaitOne( )");
gM1.WaitOne( );// Wait gM1 Release of 
Console.WriteLine("t2Start finished, gM1.WaitOne( ) satisfied");
Event2.Set( );// When the thread ends, the Event2 Set to signaled state 
public void t3Start( )
Console.WriteLine("t3Start started, Mutex.WaitAny(Mutex[])");
Mutex[] gMs = new Mutex[2];
gMs[0] = gM1;// Create 1 A Mutex Array as Mutex.WaitAny() Parameters of the method 
gMs[1] = gM2;
Mutex.WaitAny(gMs);// Wait for any in the array 1 A Mutex Object is released 
Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])");
Event3.Set( );// When the thread ends, the Event3 Set to signaled state 
public void t4Start( )
Console.WriteLine("t4Start started, gM2.WaitOne( )");
gM2.WaitOne( );// Wait gM2 Be released 
Console.WriteLine("t4Start finished, gM2.WaitOne( )");
Event4.Set( );// When the thread ends, the Event4 Set to signaled state 

For more readers interested in C # related content, please check out the topics on this site: "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.

Related articles: