About "Mutex" Learning and mutex Learning

Source: Internet
Author: User

About "Mutex" Learning and mutex Learning

 

Mutex)

A mutex lock is a mutex synchronization object, which means that only one thread can obtain it at a time.

The mutex lock can be used when a shared resource can be accessed by only one thread at a time.

 

 

When two or more threads need to access a shared resource at the same time, the system needs to use a synchronization mechanism to ensure that only one thread uses the resource at a time.

 

Mutex is a synchronization primitive that grants only one thread the exclusive access to shared resources. If a thread obtains the mutex, the second thread to obtain the mutex will be suspended until the first thread releases the mutex.

 

 

In my understanding, many people queue up for an ATM to withdraw money. They must wait until the first person gets the money, the second person can use it, and so on.

Code:

Public void AutomaticSubmitProcess (int MaxQueueStep, string System) {bool bCreateNew = false; Mutex mut = new Mutex (true, "name", out bCreateNew ); // use a Boolean value indicating whether the call thread should have the initial ownership of the mutex, // a string used as the name of the mutex, // a Boolean value indicating whether the call thread is granted the Initial ownership of the Mutex when the method returns to initialize a new instance of the Mutex class. If (bCreateNew = false) {// indicates that someone is taking money at the ATM. Please wait in line. Console. WriteLine ("Another Instance is running! "); Return;} // now you can get the money at the ATM, DataSet ds = Alb. getProcessOperationQueue (MaxQueueStep, "UBI"); if (ds. tables [0]. rows. count> 0) {// if the project is involved, no data modifications will be posted.} mut. releaseMutex (); // release Mutex once. Mut. Close (); mut. Dispose ();}

 

I used it for the first time, only explained my understanding, said wrong, and hoped to propose changes,

 

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.