C++ Boost库 多线程 线程锁mutex lock_guard unique_lockupgrade_lockupgrade_to_unique_lock实例

Posted 软件工程小施同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ Boost库 多线程 线程锁mutex lock_guard unique_lockupgrade_lockupgrade_to_unique_lock实例相关的知识,希望对你有一定的参考价值。

1 例子

多线程访问同一资源时,为了保证数据的一致性,必要时需要加锁。

1.1 直接操作 mutex,即直接调用 mutex 的 lock / unlock 函数。

#include <iostream>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>

boost::mutex mutex;
int count = 0;

void Counter() {
  mutex.lock();

  int i = ++count;
  std::cout << "count == " << i << std::endl;

  // 前面代码如有异常,unlock 就调不到了。
  mutex.unlock();
}

int main() {
  boost::thread_group threads;
  for (int i = 0; i < 4; ++i) {
    threads.create_thread(&Counter);
  }

  threads.join_all();
  return 0;
}

1.2 使用 lock_guard 自动加锁、解锁。原理是 RAII,和智能指针类似。

#include <iostream>
#include <boost/thread/lock_guard.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>

boost::mutex mutex;
int count = 0;

void Counter() {
  // lock_guard 在构造函数里加锁,在析构函数里解锁。
  boost::lock_guard<boost::mutex> lock(mutex);

  int i = ++count;
  std::cout << "count == " << i << std::endl;
}

int main() {
  boost::thread_group threads;
  for (int i = 0; i < 4; ++i) {
    threads.create_thread(&Counter);
  }

  threads.join_all();
  return 0;
}

1.3 使用 unique_lock 自动加锁、解锁。

unique_lock 与 lock_guard 原理相同,但是提供了更多功能(比如可以结合条件变量使用)。 注意:mutex::scoped_lock 其实就是 unique_lock<mutex> 的 typedef。

#include <iostream>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>

boost::mutex mutex;
int count = 0;

void Counter() {
  boost::unique_lock<boost::mutex> lock(mutex);

  int i = ++count;
  std::cout << "count == " << i << std::endl;
}

int main() {
  boost::thread_group threads;
  for (int i = 0; i < 4; ++i) {
    threads.create_thread(&Counter);
  }

  threads.join_all();
  return 0;
}

2 unique_lock和lock_guard的区别

简单的说,unique_lock相对于lock_guard,会有更多特性。 unique_lock和lock_guard都遵循RAII。

unique_lock和lock_guard最大的不同是unique_lock不需要始终拥有关联的mutex,而lock_guard始终拥有mutex。这意味着unique_lock需要利用owns_lock()判断是否拥有mutex。另外,如果要结合使用条件变量,应该使用unique_lock。

  • Lock doesn't have to taken right at the construction, you can pass the flag std::defer_lock during its construction to keep the mutex unlocked during construction.

  • We can unlock it before the function ends and don't have to necessarily wait for destructor to release it, which can be handy.

  • You can pass the ownership of the lock from a function, it is movable and not copyable.

  • It can be used with conditional variables since that requires mutex to be locked, condition checked and unlocked while waiting for a condition. 参考[StackOverflow]:https://stackoverflow.com/questions/6731027/boostunique-lock-vs-boostlock-guard

本文转载自:https://www.cnblogs.com/dengchj/p/9198121.html

Boost库——线程锁lock_guard 、unique_lock 的对比 - osc_wmf87tix的个人空间 - OSCHINA - 中文开源技术交流社区

upgrade_lock和upgrade_to_unique_lock

#include <iostream>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
 
boost::shared_mutex mutex;
int count = 0;
 
void Counter() {
  boost::upgrade_lock<boost::shared_mutex> lock(mutex);
  boost::upgrade_to_unique_lock<boost::shared_mutex> uniquelock(lock);
 
  int i = ++count;
  std::cout << "count == " << i << std::endl;
}
 
int main() {
  boost::thread_group threads;
  for (int i = 0; i < 4; ++i) {
    threads.create_thread(&Counter);
  }
 
  threads.join_all();
  return 0;
}

以上是关于C++ Boost库 多线程 线程锁mutex lock_guard unique_lockupgrade_lockupgrade_to_unique_lock实例的主要内容,如果未能解决你的问题,请参考以下文章

boost多线程库使用指南

两个线程都在调用 boost::mutex::lock() 时阻塞

C++多线程1.2-线程安全的保证——互斥量mutex(锁)和原子变量atomic

C++多线程1.2-线程安全的保证——互斥量mutex(锁)和原子变量atomic

boost的线程库和c++11的线程序有何不同

C++ boost::upgrade_lock upgrade_to_unique_lock 升级锁 是什么 怎么用