Multithreading Study Notes 4 mutex

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.

Code code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> using system;
using system. collections. generic;
using system. text;
using system. threading;

NamespaceLeleapplication1
{
Public ClassMymutex
{
Mutex M1;
Mutex m2;

Autoresetevent aut1 =   New Autoresetevent ( False );
Autoresetevent aut2 =   New Autoresetevent ( False );
Autoresetevent aut3 =   New Autoresetevent ( False );
Autoresetevent aut4 =   New Autoresetevent ( False );

Public Mymutex ()
{
M1 =   New Mutex ( True , " Mutex1 " );
M2 =   New Mutex ( True , " Mutex2 " );
}

Public   Void Start1 ()
{
Console. writeline ( " Thread1 start " );
M1.waitone ();
Console. writeline ( " Thread1 end " );
Aut1.set ();
M1.releasemutex ();
}
Public   Void Start2 ()
{
Console. writeline ( " Thread2 start " );
M2.waitone ();
Console. writeline ( " Thread2 end " );
Aut2.set ();
M2.releasemutex ();
}
Public   Void Start3 ()
{
Console. writeline ( " Thread3 start " );
Mutex [] list =   New Mutex [ 2 ];
List [ 0 ] = M1;
List [ 1 ] = M2;
Waithandle. waitall (list, timeout. infinite, True );
Console. writeline ( " Thread3 end " );
Aut3.set ();
Foreach (Mutex m In List)
{
M. releasemutex ();
}
}

Public   Void Start4 ()
{
Console. writeline ( " Thread4 start " );
Mutex [] list =   New Mutex [ 2 ];
List [ 0 ] = M1;
List [ 1 ] = M2;
Int K = Mutex. waitany (list );
Console. writeline ( " Thread4 end " );
List [K]. releasemutex ();
}

Public   Void Test ()
{
Thread Th1 =   New Thread ( New Threadstart (start1 ));
Thread 22. =   New Thread ( New Threadstart (start2 ));
Thread th3 =   New Thread ( New Threadstart (start3 ));
Thread th4 =   New Thread ( New Threadstart (start4 ));

Th1.start ();
Th2.start ();
Th3.start ();
Th4.start ();

Autoresetevent [] autolist =   New Autoresetevent [ 4 ];
Autolist [ 0 ] = Aut1;
Autolist [ 1 ] = Aut2;
Autolist [ 2 ] = Aut3;
Autolist [ 3 ] = Aut4;

console. writeline ( " m2 releasemutex " );
m2.releasemutex ();
console. writeline ( " m1 releasemutex " );
m1.releasemutex ();

Waithandle. waitall (autolist );
Console. writeline ("All set");
Console. Readline ();
}
}
}

Note that the mutex must be released each time. Otherwise, an abandonedmutexexception occurs.

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.