Multi-thread mutex usage (Classic)

Source: Internet
Author: User

You 'd better manually click it.CodeIt is helpful for understanding.

Summary
I do not know which of my predecessors wrote the following three methods:
1. If you only have a liking for a specific position, You Need To waitone (), but do not forget releasemutex (). Never waitone () Only releasemutex () twice () once (What do you do when you take over MK and don't ls? Even if there are no children, it's not good to hold down the kitten or dog ......);
2. If you like to give a lecture and want to take up two positions to handle the work, you need to waitall ([1, 2]);
3. If you don't think it's okay to go to any position, you can waitany ([1, 2])...


Mutex's waitone () function

From 1 to 2 to 3 days ago, I went to heimu cliff to find an unbeaten player in the East. When I heard the unbeaten complaints from the east, I said that my eyes were so tired all day, so I made an undefeated activity eye for the east.Program.

 Class  Program { Static   Void Main ( String  [] ARGs ){  //  To make it easier to set a smaller form Console. Required wwidth = 30 ; Console. bufferwidth = 30  ; Console. windowheight = 16 ; Console. bufferheight = 16  ; Mutex mk = New Mutex (False , "  My mutex  "  );  For ( Int I = 0 ; I < 1000 ; I ++ ) {Mk. waitone ();  For ( Int J = 0 ; J < 30 ; J ++) {Console. Write (  "  >  "  ); Thread. Sleep (  100  );} Mk. releasemutex (); thread. Sleep (  500  );}}} 

 

Two instances that run the program one by one (run a console application and then click "run" to enable the two processes), and discharge them together (as shown in ), you can see the effect of the arrow "Crossing" from the left form to the right form.

Yes, we need to synchronize two processes (the main thread in the process). This job needs to be handed over to mutex. Mutex is similar to monitor, except that monitor is. net built-in thread synchronization mechanism. mutex encapsulates the thread synchronization mechanism of the Windows operating system. The monitor speed is faster and the mutex speed is much slower than the monitor speed; monitor can only be used to synchronize threads in the same process; mutex can be used to synchronize threads belonging to different processes.

Mutex's waitall () function

Now we have expanded WC and increased mK to two, but we have encountered two rehearsal processes. Both of them have to occupy two mk at the same time to work, so the running effect is the same as that of the previous program.

 Class  Program {  Static   Void Main ( String  [] ARGs ){  //  To make it easier to set a smaller form Console. Required wwidth =30 ; Console. bufferwidth = 30  ; Console. windowheight = 16 ; Console. bufferheight = 16  ; Mutex mk1 = New Mutex ( False , "  My mutex1  "  ); Mutex Mk2 = New Mutex (False , "  My mutex2  "  ); Mutex [] mks = New  Mutex [] {mk1, Mk2 };  For ( Int I = 0 ; I < 1000 ; I ++ ) {Mutex. waitall (mks );  For ( Int J = 0 ; J < 30 ; J ++ ) {Console. Write (  "  >  "  ); Thread. Sleep (  100  );} Mk1.releasemutex (); mk2.releasemutex (); thread. Sleep (  500  );}}} 


Mutex's waitany () function

Check out this applet:

 Class  Program {  Static   Void Main ( String  [] ARGs ){  //  To make it easier to set a smaller form Console. Required wwidth = 30 ; Console. bufferwidth = 30  ; Console. windowheight =16 ; Console. bufferheight = 16  ; Mutex mk1 = New Mutex ( False , "  My mutex1  "  ); Mutex Mk2 = New Mutex ( False , "  My mutex2  " ); Mutex [] mks = New  Mutex [] {mk1, Mk2 };  For ( Int I = 0 ; I < 1000 ; I ++ ){  Int Index = mutex. waitany (mks ); //  Return the index of MK occupied by this process in mks. Console. Write ( "  Index: " + Index. tostring ());  For ( Int J = 0 ; J < 30 ; J ++ ) {Console. Write (  "  >  "  ); Thread. Sleep (  100  );} Mks [Index]. releasemutex (); thread. Sleep ( New Random (). Next ( 100 , 3000  ));}}} 

If two instances of this program are running at the same time, as described in the abstract in this article, as long as one of mk1 and Mk2 is idle, the process can go in and handle the affairs, therefore, the two processes can output the ">" character at the same time. Note that the program's 24th lines, each process will randomly sleep 100 to 3000 milliseconds after 30 ">" characters are output, so that mk1 and Mk2 may be idle at the same time, therefore, process 1 occupies mk1 while process 2 occupies Mk2. Process 1 occupies Mk2 while process 2 occupies mk1 (marked with green and red lines respectively ).

 

Reference: http://www.cnblogs.com/1-2-3/articles/1212391.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.