AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
# 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; } ~~~ ![](https://img.kancloud.cn/f9/ac/f9ac965048039a7112d033c929706b01_1447x746.png) 结果如上: 1. notify只能唤醒一个等待线程 2. notifyAll能唤醒所有等待的线程,线程间不安全 3. wait\_seconds超时自动解锁