Boost: thread lock

Source: Internet
Author: User

1. Overview of boost locks:

The boost Library provides the mutex and lock classes. By combining them, you can easily build read/write locks and mutex locks.

 

2. mutex object class (two main types ):

1. boost: mutex (exclusive mutex class) --> There are lock and unlock methods

2. boost: shared_mutex (shared mutex class) --> There are lock And unlock methods And shared_lock And shared_unlock methods.

 

3. lock template class:

1. boost: unique_lock <T> (exclusive lock): T in unique_lock <T> can be any of the mutex classes. The lock and unlock methods are automatically called.

2. boost: shared_lock <T> (shared lock): The T in shared_lock <T> can only be shared_mutex class; The shared_lock and shared_unlock methods are automatically called.

 

4. Recursive mutex:

  

Boost: mutex: scoped_lock

Boost: mutex io_mutex;

Void foo ()

{

{

Boost: mutex: scoped_lock lock (io_mutex); // lock

} // Unlock

}

 

5. Set lock timeout:

Boost: unique_lock <boost: timed_mutex> lk (io_mutex, std: chrono: milliseconds (3); // timeout for 3 seconds

Related Article

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.