There is an area lock Lock_guard in the c++11, and a second zone lock Unique_lock.
The area lock Lock_guard is simple to use and is valid throughout the region except for the constructor function, which has no other member function.
In addition to the Lock_guard function, the area lock Unique_guard provides more member_function and is relatively flexible.
The most useful set of functions for Unique_guard are:
Lock |
Locks the associated mutex (Public member function) |
Try_lock |
Tries to lock the associated mutex, returns if the mutex isn't available (Public member function) |
Try_lock_for |
Attempts to lock the associated timedlockable mutex, returns if the mutex has been for the unavailable time specified tion (Public member function) |
Try_lock_until |
Tries to lock the associated timedlockable mutex, returns if the mutex has been unavailable until specified time point has Been reached (Public member function) |
Unlock |
Unlocks the associated mutex |
Through the above function, the lock/unlock can be more flexible control of the range of locks, reduce the size of the lock.
By Try_lock_for/try_lock_until, you can control the wait time for locking, which is an optimistic lock.
The above is a superficial understanding, there are new ideas in addition.