Mutex experiences and file sharing read/write locks

Source: Internet
Author: User

Mutex experiences and file sharing read/write locks

Mutex allows you to create/open a system named object for synchronization between threads and processes.

Precautions for using mutex:

1. Use mutex M = new mutex (true, "123", out createnew); when creating an instance, because the first parameter (initiallyowned) passes true, if the return value of createnew is true, the instance receives the processing signal by default. At this time, waitone is not required. This is only required when createnew returns false.

Therefore, the response code should be as follows:

Bool createnew = false;
Mutex M = new mutex (true, "123", out createnew );
If (! Createnew)
M. waitone ();

......

M. releasemutex ();

 

2. waitone must be called in pairs with releasemutex. However, it is easy to be negligent. Besides the first point mentioned above, there is also: When waitone sets a timeout and returns a timeout value, releasemutex should not be called.

Bool waitreture = false;

Waitreture = M. waitone (10*1000, false );

If (waitreture)
M. releasemutex ();

3. Then, calling the close function completely releases the system resources occupied only when the reference count of the system object is 0.

 

I wrote a file sharing read/write lock, which will be attached later.

 

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.