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.