如何在C++中實現(xiàn)一個正確的時間循環(huán)器詳解
前言
實際工程中可能會有這樣一類普遍需求:在服務(wù)中,單獨起一個線程,以一個固定的時間間隔,周期性地完成特定的任務(wù)。我們把這種問題抽象成一個時間循環(huán)器。
Naive Way
class TimerCircle { private: std::atomic_bool running_{false}; uint64_t sleep_{0UL}; std::thread thread_; public: explicit TimerCircle(uint64_t s) : sleep_{s} {} ~TimerCircle() { if (thread_.joinable()) { terminate(); thread_.join(); } } TimerCircle(const TimerCircle&) = delete; TimerCircle& operator=(const TimerCircle&) = delete; TimerCircle(TimerCircle&&) = default; TimerCircle& operator=(TimerCircle&&) = default; public: void launch() { thread_ = std::move(std::thread(&TimerCircle::loop, this)); } void terminate() { running_.store(false); } void loop() { running_.store(true); while (running_.load()) { do_something(); std::this_thread::sleep_for(std::chrono::seconds(sleep_)); } } private: void do_something() const = 0; };
實現(xiàn)簡單平凡,一眼就能看出來沒啥問題,于是也沒啥好說的。
細節(jié)里的魔鬼
唯一的魔鬼藏在細節(jié)里。如果 TimerCircle 類型的對象發(fā)生析構(gòu),那么析構(gòu)該對象的線程最多會被阻塞 sleep_ 秒。如果周期很長,比如長達 6 小時,那這顯然是不可接受。
為此,我們需要借助標準庫的條件變量 std::condition_variable 的 wait_for 函數(shù)的幫助。首先看其函數(shù)簽名
template <typename Rep, typename Period, typename Predicate> bool wait_for(std::unique_lock<std::mutex>& lock, const std::chrono::duration<Rep, Period>& rel_time, Predicate pred);
函數(shù)接受三個參數(shù)。lock 是一個 unique_lock,它必須為調(diào)用 wait_for 的線程所鎖??;rel_time 是一個時間段,表示超時時間;pred 是一個謂詞,它要么返回 true 要么返回 false。
一旦調(diào)用,函數(shù)會阻塞當前線程,直到兩種情況返回:
- 超時;此時函數(shù)返回 pred()。
- 條件變量被通知,且謂詞返回 true;此時函數(shù)返回 true。
于是我們可以實現(xiàn)一個 Countdown 類
#include <chrono> #include <condition_variable> #include <mutex> class Countdown final { private: bool running_ = true; mutable std::mutex mutex_; mutable std::condition_variable cv_; public: Countdown() = default; ~Countdown() = default; Countdown(const Countdown&) = delete; Countdown& operator=(const Countdown&) = delete; Countdown(Countdown&&) = delete; Countdown& operator=(Countdown&&) = delete; public: void terminate() { { std::lock_guard<std::mutex> lock(mutex_); running_ = false; } cv_.notify_all(); } template <typename Rep, typename Peroid> bool wait_for(std::chrono::duration<Rep, Peroid>&& duration) const { std::unique_lock<std::mutex> lock(mutex_); bool terminated = cv_.wait_for(lock, duration, [&]() { return !running_; }); return !terminated; } };
于是,TimerCircle 就變成
class TimerCircle { private: uint64_t sleep_{0UL}; Countdown cv_; std::thread thread_; public: explicit TimerCircle(uint64_t s) : sleep_{s} {} ~TimerCircle() { if (thread_.joinable()) { terminate(); thread_.join(); } } TimerCircle(const TimerCircle&) = delete; TimerCircle& operator=(const TimerCircle&) = delete; TimerCircle(TimerCircle&&) = default; TimerCircle& operator=(TimerCircle&&) = default; public: void launch() { thread_ = std::move(std::thread(&TimerCircle::loop, this)); } void terminate() { cv_.terminate(); } void loop() { while (cv_.wait_for(std::chrono::seconds(sleep_))) { do_something(); } } private: void do_something() const = 0; };
簡單,明了。
總結(jié)
到此這篇關(guān)于如何在C++中實現(xiàn)一個正確的時間循環(huán)器的文章就介紹到這了,更多相關(guān)C++實現(xiàn)時間循環(huán)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C語言求兩個數(shù)的最大公約數(shù)及最小公倍數(shù)的方法
這篇文章主要介紹了C語言求兩個數(shù)的最大公約數(shù)及最小公倍數(shù)的方法,輾轉(zhuǎn)相除法和輾轉(zhuǎn)相減法在解決這種問題時最常用到,需要的朋友可以參考下2016-03-03C/C++ break和continue區(qū)別及使用方法
這篇文章主要介紹了C/C++ break和continue區(qū)別及使用方法的相關(guān)資料,需要的朋友可以參考下2017-07-07c++函數(shù)中的指針參數(shù)與地址參數(shù)區(qū)別介紹
c++函數(shù)中的指針參數(shù)與地址參數(shù)區(qū)別介紹;可供參考2012-11-11