# Condition
### 头文件
~~~
#ifndef _LEO_CONDITION_H_
#define _LEO_CONDITION_H_
#include "Mutex.h"
#include "Noncopyable.h"
#include <pthread.h>
namespace leo {
class Condition : public Noncopyable {
public:
explicit Condition(Mutex& mutex);
~Condition();
void wait();
bool wait_seconds(time_t seconds);
void notify();
void notifyAll();
private:
Mutex& mutex_;
pthread_cond_t cond_;
};
}
#endif
~~~
### 实现
~~~
#include "Condition.h"
#include <errno.h>
#include <stdint.h>
namespace leo {
Condition::Condition(Mutex& mutex)
: mutex_(mutex) {
pthread_cond_init(&cond_, nullptr);
}
Condition::~Condition() {
pthread_cond_destroy(&cond_);
}
void Condition::wait() {
pthread_cond_wait(&cond_, mutex_.getMutex());
}
bool Condition::wait_seconds(time_t seconds) {
struct timespec abstime;
clock_gettime(CLOCK_REALTIME, &abstime);
const static int64_t kNanoSecondsPerSecond = 1000000000;
int64_t nanoseconds = static_cast<int64_t>(seconds * kNanoSecondsPerSecond);
abstime.tv_sec += static_cast<time_t>((abstime.tv_nsec + nanoseconds) / kNanoSecondsPerSecond);
abstime.tv_nsec = static_cast<long>((abstime.tv_nsec + nanoseconds) % kNanoSecondsPerSecond);
return ETIMEDOUT == pthread_cond_timedwait(&cond_, mutex_.getMutex(), &abstime);
}
void Condition::notify() {
pthread_cond_signal(&cond_);
}
void Condition::notifyAll() {
pthread_cond_broadcast(&cond_);
}
}
~~~
### clock\_gettime
int clock\_gettime(clockid\_t clk\_id, struct timespec\* tp);
可以根据需要,获取不同要求的精确时间
~~~
struct timespec
{
time_t tv_sec; /* 秒 */
long tv_nsec; /* 纳秒 */
};
~~~
### 测试
~~~
#include "Condition.h"
#include "Log.h"
#include "Thread.h"
#include <memory>
#include <unistd.h>
#include <vector>
#include "Mutex.h"
using namespace leo;
int count = 0;
void notify_test() {
Mutex mutex;
Condition cond(mutex);
std::vector<std::shared_ptr<Thread>> v;
for (int i = 0; i < 10; ++i) {
v.push_back(std::make_shared<Thread>([&]() {
MutexGuard guard(mutex);
cond.wait();
LOG_DEBUG << "+1";
}));
}
for (auto& i : v) {
i->start();
}
cond.notify();
LOG_DEBUG << "----------";
sleep(2);
LOG_DEBUG << "==========";
}
void notifyAll_test() {
Mutex mutex;
Condition cond(mutex);
std::vector<std::shared_ptr<Thread>> v;
for (int i = 0; i < 10; ++i) {
v.push_back(std::make_shared<Thread>([&]() {
MutexGuard guard(mutex);
cond.wait();
count++;
LOG_DEBUG << "+1";
}));
}
for (auto& i : v) {
i->start();
}
cond.notifyAll();
LOG_DEBUG << "----------";
sleep(2);
LOG_DEBUG << "==========";
LOG_DEBUG << "count=" << count;
}
void wait_seconds_test() {
Mutex mutex;
Condition cond(mutex);
std::vector<std::shared_ptr<Thread>> v;
for (int i = 0; i < 1; ++i) {
v.push_back(std::make_shared<Thread>([&]() {
MutexGuard guard(mutex);
cond.wait_seconds(2);
LOG_DEBUG << "wait up";
}));
}
for (auto& i : v) {
i->start();
}
LOG_DEBUG << "----------";
sleep(5);
LOG_DEBUG << "==========";
}
int main() {
Singleton<Logger>::getInstance()->addAppender("console", LogAppender::ptr(new ConsoleAppender()));
LOG_DEBUG << "test notify ..........";
notify_test();
LOG_DEBUG << "test notifyAll ..........";
notifyAll_test();
LOG_DEBUG << "test wait_seconds ..........";
wait_seconds_test();
return 0;
}
~~~

结果如上:
1. notify只能唤醒一个等待线程
2. notifyAll能唤醒所有等待的线程,线程间不安全
3. wait\_seconds超时自动解锁
- 空白目录
- 算法
- 排序
- 冒泡排序
- 选择排序
- 插入排序
- 归并排序
- 快速排序
- 计数排序
- 桶排序
- 基数排序
- 希尔排序
- 堆排序
- 二分查找
- 最小堆
- 最小索引堆
- 平衡二叉树(AVL tree)
- bitmap位图
- 布隆过滤器
- hashmap
- topK
- 跳表
- LRU Cache
- kmp
- 最小堆和堆排序
- 最短路径
- C++
- 运行时类型判断RTTI
- C++反射
- 手动实现智能指针
- 序列化实现
- rpc实现
- std::forward
- 函数指针的妙用
- C/C++
- std::function
- 同步队列
- 线程池实现
- std::promise
- 深入理解虚函数
- extern "C" 关键字讲解
- 大端小端的区别
- 简历
- 简历1
- redis
- 数据结构和对象
- sds
- list
- zskiplist
- 腾讯云redis面试题总结
- redis集群部署
- LeetCode
- 目标
- go基础
- 算法快速入门
- 数据结构篇
- 二叉树
- 链表
- 栈和队列
- 二进制
- 基础算法篇
- 二分搜索
- 排序算法
- 动态规划
- 算法思维
- 递归思维
- 滑动窗口思想
- 二叉搜索树
- 回溯法
- 其他
- 剑指offer
- 笔记
- git代理加速
- Linux
- vim大法
- vscode远程不能跳转
- cmake
- 设计模式
- 单例模式
- 简单工厂模式
- 外观模式
- 适配器模式
- 工厂方法模式
- 抽象工厂模式
- 生成器模式
- 原型模式
- 中介者模式
- 观察者模式
- 访问者模式
- 命令模式
- 网络编程
- epoll reactor模式
- linux timerfd系列函数总结
- IO
- mapreduce
- 反射器
- leo通信库
- Mutex
- Condition
- thread
- raft
- 协程
- hook
- 定时器
- 别人的面试经验
- 面试题
- vector崩溃问题
- JAVA
- Linux java环境配置
- ucore
- lab1
- FreeNOS
- leveldb
- 刷题笔记
- 回文串
- 前缀树
- 字符串查找
- 查找两个字符串a,b中的最长公共子串
- 动态规划
- golang
- 顺序循环打印实现
- 数据结构
- rpc运用
- python
- 单例
- 深拷贝浅拷贝
- 链表
- python基础题
- mysql
- 事务
- Linux
- 共享内存
- 刷题记录
- 贪心算法
- 动态规划
- 面试
- 腾讯C++面试
- 微众面试JD
- 迅雷网络面试
- 学习网址
- rabbitMq
