Thread in boost library threads 2--mutex and lock

Source: Internet
Author: User

1. Mutex Object class

There are two main types of mutex classes: exclusive and shared mutex.
▲ Exclusive Mutex Amount:
Mutex: Exclusive mutex, which is the simplest and most common type of mutex
Try_mutex: It is a synonym for a mutex, provided for compatibility with previous versions
Timed_mutex: It is also an exclusive mutex, but provides a timeout lock feature
▲ Recursive mutual-exclusion amount:
Recursive_mutex: Recursive mutex, can be locked multiple times, corresponding to the number of unlocked
Recursive_try_mutex: It is a synonym for Recursive_mutex, provided for compatibility with previous versions
Recursive_timed_mutex: It is also recursive mutex, basic ability with Recursive_mutex, but provides time-out lock function
▲ Shared Mutex:
Shared_mutex:multiple-reader/single-writer shared Mutex (also known as read-write lock).
The mutex has a lock and unlock method, Shared_mutex in addition to providing the lock and unlock methods, there are Shared_lock and Shared_unlock methods.

2. Lock template class

▲ Exclusive Lock:
Boost::unique_lock<t>, where T can be any of a mutex.
If T is a mutex, then Boost::unique_lock<boost::mutex>, and the lock and unlock methods are automatically called when you construct and refactor.
If T is Shared_mutex, then Boost::unique_lock<boost::shared_mutex>, and the Shared_lock and shared_ of Shared_mutex are called respectively when constructing and destruction Unlock method.
Note: Scoped_lock is also an exclusive lock, which is defined in the source code as follows;
typedef unique_lock<mutex> Scoped_lock;
typedef unique_lock<timed_mutex> Scoped_timed_lock;
▲ Shared Lock:
Boost::shared_lock<t>, where T can only be the Shared_mutex class.
Of course there are some other locks: lock_guard, upgrade_lock and so on.

3. Implementation of read-write lock [CPP]View Plaincopyprint?
  1. typedef boost::shared_lock<boost::shared_mutex> Readlock;
  2. typedef boost::unique_lock<boost::shared_mutex> Writelock;
  3. Boost::shared_mutex Rwmutex;
  4. void ReadOnly ()
  5. {
  6. Readlock Rdlock (Rwmutex);
  7. //Do something
  8. }
  9. void WriteOnly ()
  10. {
  11. Writelock Wtlock (Rwmutex);
  12. //Do something
  13. }

For the same Rwmutex, threads can have multiple readlock at the same time, and these readlock block any thread that attempts to acquire Writelock until all Readlock objects are refactored. If Writelock first obtains the Rwmutex, it blocks any thread that attempts to gain Readlock or Writelock on Rwmutex.

4. The difference between boost::lock_guard<> and boost::unique_lock<> [CPP]View Plaincopyprint?
  1. Boost::mutex m;
  2. void Foo ()
  3. {
  4. Boost::lock_guard<boost::mutex> LK (m);
  5. Process (data);
  6. };
  7. Lock_guard can only be used as above, while Unique_lock allows setting timeouts, delaying locking lock, and unlock before objects are destroyed.
  8. {
  9. Boost::unique_lock<boost::mutex> LK (m);
  10. Process (data);
  11. Lk.unlock ();
  12. // do other thing
  13. };
  14. Set lock timeout
  15. {
  16. Boost::unique_lock<boost::timed_mutex> lk (M, Std::chrono::milliseconds (3)); //Timeout 3 seconds
  17. if (LK)
  18. Process (data);
  19. };

5. Simple example [CPP]View Plaincopyprint?
    1. Namespace
    2. {
    3. Boost::mutex Mutex;
    4. Boost::shared_mutex Shared_mutex;
    5. void Wait (int seconds)
    6. {
    7. Boost::this_thread::sleep (boost::p osix_time::seconds (seconds));
    8. }
    9. void Threadfun1 ()
    10. {
    11. For (int i = 0; i < 5; ++i)
    12. {
    13. Wait (1);
    14. Mutex.lock ();
    15. Print_debug (i);
    16. Mutex.unlock ();
    17. }
    18. }
    19. void threadfun2 ()
    20. {
    21. For (int i = 0; i < 5; ++i)
    22. {
    23. Wait (1);
    24. Boost::lock_guard<boost::mutex> lock (mutex);
    25. Print_debug (i);
    26. }
    27. }
    28. void Threadfun3 ()
    29. {
    30. For (int i = 0; i < 5; ++i)
    31. {
    32. Wait (1);
    33. //unique_lock<boost::mutex> = Scoped_lock
    34. Boost::unique_lock<boost::mutex> lock (mutex);
    35. Std::cout << lock.owns_lock () << Std::endl;
    36. Print_debug (i);
    37. }
    38. }
    39. }
    40. 1. Mutex examples
    41. void Test_thread_syn1 ()
    42. {
    43. Boost::thread T1 (&THREADFUN1);
    44. Boost::thread T2 (&THREADFUN1);
    45. T1.join ();
    46. T2.join ();
    47. }
    48. 2. Lock_guard Example
    49. void Test_thread_syn2 ()
    50. {
    51. Boost::thread T1 (&threadfun2);
    52. Boost::thread T2 (&THREADFUN2);
    53. T1.join ();
    54. T2.join ();
    55. }
    56. 3. scoped_lock Example
    57. void Test_thread_syn3 ()
    58. {
    59. Boost::thread T1 (&THREADFUN3);
    60. Boost::thread T2 (&THREADFUN3);
    61. T1.join ();
    62. T2.join ();
    63. }

Thread in boost library threads 2--mutex and lock

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.