C++11 Mutex Unique_lock and Lock_guard differences

Source: Internet
Author: User
Tags mutex

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.

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.