最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何在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)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

實(shí)際工程中可能會(huì)有這樣一類普遍需求:在服務(wù)中,單獨(dú)起一個(gè)線程,以一個(gè)固定的時(shí)間間隔,周期性地完成特定的任務(wù)。我們把這種問題抽象成一個(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)簡單平凡,一眼就能看出來沒啥問題,于是也沒啥好說的。

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

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

為此,我們需要借助標(biāo)準(zhǔn)庫的條件變量 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 的線程所鎖?。籸el_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 類

#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++中實(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)文章

  • Opencv2.4.9函數(shù)HoughLinesP分析

    Opencv2.4.9函數(shù)HoughLinesP分析

    這篇文章主要為大家詳細(xì)介紹了Opencv2.4.9函數(shù)HoughLinesP,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • QT開發(fā)應(yīng)用程序的歡迎界面實(shí)例

    QT開發(fā)應(yīng)用程序的歡迎界面實(shí)例

    下面小編就為大家?guī)硪黄猀T開發(fā)應(yīng)用程序的歡迎界面實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • C語言volatile關(guān)鍵字的作用與示例

    C語言volatile關(guān)鍵字的作用與示例

    這篇文章主要介紹了C語言volatile關(guān)鍵字的作用,volatile提醒編譯器它后面所定義的變量隨時(shí)都有可能改變,因此編譯后的程序每次需要存儲(chǔ)或讀取這個(gè)變量的時(shí)候,都會(huì)直接從變量地址中讀取數(shù)據(jù)
    2023-04-04
  • C語言 TerminateProcess函數(shù)案例詳解

    C語言 TerminateProcess函數(shù)案例詳解

    這篇文章主要介紹了C語言 TerminateProcess函數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 老生常談C++中實(shí)參形參的傳遞問題

    老生常談C++中實(shí)參形參的傳遞問題

    下面小編就為大家?guī)硪黄仙U凜++中實(shí)參形參的傳遞問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • C語言實(shí)現(xiàn)三角函數(shù)表

    C語言實(shí)現(xiàn)三角函數(shù)表

    這篇文章主要為大家詳細(xì)介紹了C語言三角函數(shù)表,打印出相對(duì)應(yīng)的三角函數(shù)值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • 分享常用的3個(gè)C++小技巧

    分享常用的3個(gè)C++小技巧

    這篇文章主要分享了常用的3個(gè)C++小技巧,
    2021-12-12
  • C語言實(shí)現(xiàn)簡單計(jì)算器功能(2)

    C語言實(shí)現(xiàn)簡單計(jì)算器功能(2)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單計(jì)算器功能的第二部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Visual Studio Code (vscode) 配置C、C++環(huán)境/編寫運(yùn)行C、C++的教程詳解(Windows)【真正的小白版】

    Visual Studio Code (vscode) 配置C、C++環(huán)境/編寫運(yùn)行C、C++的教程詳解(Windows

    這篇文章主要介紹了Visual Studio Code (vscode) 配置C、C++環(huán)境/編寫運(yùn)行C、C++的教程詳解(Windows)【真正的小白版】,圖文詳解介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • C++代碼規(guī)范之命名規(guī)則

    C++代碼規(guī)范之命名規(guī)則

    以下是對(duì)C++中的命名規(guī)則進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07

最新評(píng)論

西林县| 灌云县| 莱州市| 青龙| 永寿县| 大宁县| 绩溪县| 曲阳县| 宁波市| 宜丰县| 湘潭市| 敦化市| 隆昌县| 古田县| 鹤峰县| 玉林市| 自贡市| 全州县| 汉川市| 松滋市| 鄢陵县| 丰城市| 南和县| 云林县| 毕节市| 虹口区| 合江县| 怀安县| 洛阳市| 乐都县| 丹棱县| 祁连县| 长汀县| 麻栗坡县| 阳朔县| 建宁县| 公主岭市| 安岳县| 临海市| 绥江县| 邳州市|