欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在C++中實(shí)現(xiàn)一個(gè)正確的時(shí)間循環(huán)器詳解

 更新時(shí)間:2020年10月15日 09:25:07   作者:始終  
這篇文章主要給大家介紹了關(guān)于如何在C++中實(shí)現(xiàn)一個(gè)正確的時(shí)間循環(huán)器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

實(shí)際工程中可能會(huì)有這樣一類(lèi)普遍需求:在服務(wù)中,單獨(dú)起一個(gè)線程,以一個(gè)固定的時(shí)間間隔,周期性地完成特定的任務(wù)。我們把這種問(wèn)題抽象成一個(gè)時(shí)間循環(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;
};

實(shí)現(xiàn)簡(jiǎn)單平凡,一眼就能看出來(lái)沒(méi)啥問(wèn)題,于是也沒(méi)啥好說(shuō)的。

細(xì)節(jié)里的魔鬼

唯一的魔鬼藏在細(xì)節(jié)里。如果 TimerCircle 類(lèi)型的對(duì)象發(fā)生析構(gòu),那么析構(gòu)該對(duì)象的線程最多會(huì)被阻塞 sleep_ 秒。如果周期很長(zhǎng),比如長(zhǎng)達(dá) 6 小時(shí),那這顯然是不可接受。

為此,我們需要借助標(biāo)準(zhǔn)庫(kù)的條件變量 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ù)接受三個(gè)參數(shù)。lock 是一個(gè) unique_lock,它必須為調(diào)用 wait_for 的線程所鎖住;rel_time 是一個(gè)時(shí)間段,表示超時(shí)時(shí)間;pred 是一個(gè)謂詞,它要么返回 true 要么返回 false。

一旦調(diào)用,函數(shù)會(huì)阻塞當(dāng)前線程,直到兩種情況返回:

  • 超時(shí);此時(shí)函數(shù)返回 pred()。
  • 條件變量被通知,且謂詞返回 true;此時(shí)函數(shù)返回 true。

于是我們可以實(shí)現(xiàn)一個(gè) Countdown 類(lèi)

#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ǎn)單,明了。

總結(jié)

到此這篇關(guān)于如何在C++中實(shí)現(xiàn)一個(gè)正確的時(shí)間循環(huán)器的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)時(shí)間循環(huán)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論