Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the C # Mutex object

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use C # Mutex objects". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use C# Mutex objects.

How to control how many threads relate to each other without conflict and repetition requires the use of mutexes, that is, the Mutex class in the System.Threading namespace.

We can think of Mutex as a taxi and passengers as threads. Passengers first wait for the bus, then get on the bus, and get off. When a 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 relationship between the thread and 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. In the meantime, other threads that want to get the C # Mutex object have to wait.

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

An object of the AutoResetEvent class is also used, which can be thought of as a semaphore. Its signaled state is used here 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 = 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 ("MutexSample") / / 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; / / define AutoResetEvent objects 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 gM1's release 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 end conditions meet Thread.Sleep (1000); Console.WriteLine ("- Main releases gM2"); gM2.ReleaseMutex () / Thread T1 Mutex.WaitAll T4 end conditions meet / / wait 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 an Mutex array as the parameter of the Mutex.WaitAll () method gMs [1] = gM2; Mutex.WaitAll (gMs); / / wait for both 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 (); / / wait for the 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 an Mutex array as the parameter gMs [1] = gM2; Mutex.WaitAny (gMs) of the Mutex.WaitAny () method; / / wait for any Mutex object in the array to be released Console.WriteLine ("t3Start finished, Mutex.WaitAny (Mutex [])"); Event3.Set (); / / Thread ends, setting Event3 to signaled 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 (); / / Thread ends, set Event4 to signal state} so far, I believe you have a deeper understanding of "how to use the C# Mutex object", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report