等待一个时间或其他条件

在一个线程等待完成任务时,会有很多选择:

1. 它可以持续的检查共享数据标志(用于做保护工作的互斥量),直到另一个线程完成工作时对这个标志进行重设。缺点:资源浪费,开销大

2. 在等待线程的检查间隙,使用std::this_thread::sleep_for()进行周期性的间歇。 缺点:休眠时间抉择困难

bool flag;
std::mutex m; void wait_for_flag()
{
std::unique_lock<std::mutex> lk(m);
while(!flag)
{
lk.unlock(); // 1 解锁互斥量
std::this_thread::sleep_for(std::chrono::milliseconds()); // 2 休眠100ms
lk.lock(); // 3 再锁互斥量
}
}

3. 使用C++标准库提供的工具去等待事件的发生。通过另一线程触发等待事件的机制是最基本的唤醒方式,这种机制就称为“条件变量”。

C++标准库对条件变量有两套实现:std::condition_variablestd::condition_variable_any。这两个实现都包含在<condition_variable>头文件的声明中。两者都需要与一个互斥量一起才能工作(互斥量是为了同步)

std::condition_variable:只能与std::mutex一起工作,开销少

std::condition_variable_any:可以和任何满足最低标准的互斥量一起工作,开销大

std::condition_variable 提供两个重要的接口:notify_one()wait()。wait()可以让线程陷入休眠状态,notify_one()就是唤醒处于wait中的其中一个条件变量(可能当时有很多条件变量都处于wait状态)。

template<typename Predicate>
wait(std::unique_lock<std::mutex>& lk, Predicate pred)

wait()会去检查这些条件(通过调用所提供的函数),当条件满足(调用所提供的函数返回true)时返回。如果条件不满足(调用所提供的函数返回false),wait()函数将解锁互斥量,并且将这个线程置于阻塞或等待状态。另外一个线程调用notify_one()通知条件变量时,线程从睡眠状态中苏醒,重新获取互斥锁,并且再次检查条件是否满足。

std::condition_variable::wait的一个最小化实现:

template<typename Predicate>
void minimal_wait(std::unique_lock<std::mutex>& lk,Predicate pred){
while(!pred()){
lk.unlock();
lk.lock();
}
}

考虑一个生产者消费者模型:一个线程往队列中放入数据,一个线程往队列中取数据,取数据前需要判断一下队列中确实有数据,由于这个队列是线程间共享的,所以,需要使用互斥锁进行保护,一个线程在往队列添加数据的时候,另一个线程不能取,反之亦然。用互斥锁实现如下:

#include <iostream>
#include <deque>
#include <thread>
#include <mutex> std::deque<int> q;
std::mutex mu; void function_1() {
int count = ;
while (count > ) {
std::unique_lock<std::mutex> locker(mu);
q.push_front(count);
locker.unlock();
std::this_thread::sleep_for(std::chrono::seconds());
count--;
}
} void function_2() {
int data = ;
while ( data != ) {
std::unique_lock<std::mutex> locker(mu);
if (!q.empty()) {
data = q.back();
q.pop_back();
locker.unlock();
std::cout << "t2 got a value from t1: " << data << std::endl;
} else {
locker.unlock();
}
}
}
int main() {
std::thread t1(function_1);
std::thread t2(function_2);
t1.join();
t2.join();
return ;
}

问题在于,如果生产者的速度比较慢,代码中每隔1s才会有一次数据生产,这时消费者都要去获取锁-->判断队列里是否有数据-->释放锁,这个过程就是资源的浪费,无用功使得cpu占用率很高。

使用std::this_thread::sleep_for()来对代码进行改造:

void function_2() {
int data = ;
while ( data != ) {
std::unique_lock<std::mutex> locker(mu);
if (!q.empty()) {
data = q.back();
q.pop_back();
locker.unlock();
std::cout << "t2 got a value from t1: " << data << std::endl;
} else {
locker.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds());
}
}
}

这样可以减低cpu占用率,但问题在于在实际操作中如何选择休眠时间,太长或者太短都不好。

最后可以使用条件变量来对这个代码进行改造:

#include <iostream>
#include <deque>
#include <thread>
#include <mutex>
#include <condition_variable> std::deque<int> q;
std::mutex mu;
std::condition_variable cond; void function_1() {
int count = ;
while (count > ) {
std::unique_lock<std::mutex> locker(mu);
q.push_front(count);
locker.unlock();
cond.notify_one(); // Notify one waiting thread, if there is one.
std::this_thread::sleep_for(std::chrono::seconds());
count--;
}
} void function_2() {
int data = ;
while ( data != ) {
std::unique_lock<std::mutex> locker(mu);
cond.wait(locker, [](){ return !q.empty();} ); // Unlock mu and wait to be notified
data = q.back();
q.pop_back();
locker.unlock();
std::cout << "t2 got a value from t1: " << data << std::endl;
}
}
int main() {
std::thread t1(function_1);
std::thread t2(function_2);
t1.join();
t2.join();
return ;
}

需要注意的几点:

在配合条件变量使用锁时,使用std::unique_lock比std::lock_guard合适,因为在wait内部有对锁的unlock和lock操作

使用细粒度锁,尽量减小锁的范围,在notify_one()的时候,不需要处于互斥锁的保护范围内,所以在唤醒条件变量之前可以将锁unlock()

参考资料:

https://www.jianshu.com/p/c1dfa1d40f53

https://chenxiaowei.gitbook.io/c-concurrency-in-action-second-edition-2019/4.0-chinese/4.1-chinese

最新文章

  1. radio 切换内容
  2. 使用svn分支
  3. Dynamic Method Resolution
  4. 菜鸟学java开篇
  5. poj 2786 - Keep the Customer Satisfied
  6. U14Linux的帐号与用户组
  7. JBoss + EJB3 + MySql : 开发第一个EJB
  8. vue-cli 前端开发,后台接口跨域代理调试问题
  9. Git 企业开发者教程
  10. web1 - HTML&amp;CSS
  11. Kaggle新手入门之路
  12. jenkins忘记密码怎么办?
  13. SpringMVC+Apache Shiro+JPA(hibernate)案例教学(四)基于Shiro验证用户权限,且给用户授权
  14. Vs 发布编译问题
  15. 第六章Django
  16. Service Mesh
  17. rails 杂记 - model 中的exists?
  18. EOF \n \0 NULL 之间的区别
  19. thinkphp 3.2 加载第三方库 第三方命名空间库
  20. Oracle EBS 清理归档

热门文章

  1. J2EE知识总结——面试、笔试
  2. datatable 和实体互转
  3. 微信长按识别二维码,在 vue 项目中的实现
  4. [BZOJ4804]欧拉心算:线性筛+莫比乌斯反演
  5. C# Cache缓存的应用
  6. Java数据结构之排序---希尔排序
  7. [UPC10525]:Dove打扑克(暴力+模拟)
  8. springBoot 配置url 访问图片
  9. 通过构造器配置Bean
  10. 4、kubernetes资源清单快速入门190625