c++ - 在 C++ 中实现计时器/自动收报机类需要帮助

标签 c++ multithreading timer cross-platform ticker

需要帮助来实现具有开始/停止/暂停功能、可分配回调 (onTick) 的代码类,并在每个间隔跨度上在单独的线程上执行。间隔跨度是可指定和可更新的。希望它应该是跨平台的。

这是我天真的尝试,但效果并不好(start() 中的 while 循环目前正在阻塞,但理想情况下它应该在单独的线程上运行,但我不知道如何实现它)因为我C++ 多线程模型中的菜鸟:

#include <cstdint>
#include <functional>
#include <chrono>
#include <thread>
#include <future>

class Ticker {
public:
    typedef std::chrono::duration<int64_t, std::nano> tick_interval_t;
    typedef std::function<void()> on_tick_t;

    Ticker (std::function<void()> onTick, std::chrono::duration<int64_t, std::nano> tickInterval) 
    : _onTick (onTick)
    , _tickInterval (tickInterval)
    , _running (false) {}
    ~Ticker () {}

    void start () {
        if (_running) return;
        _running = true;
        while (_running) {
            std::async( std::launch::async, _onTick );
            std::this_thread::sleep_for( _tickInterval );
        }
    }
    void stop () { _running = false; }

private:
    on_tick_t           _onTick;
    tick_interval_t     _tickInterval;
    bool                _running;
};

我的尝试完全错了还是非常接近?

最佳答案

考虑以下代码和用法示例。我希望我理解正确。 只需在单独的线程中运行 while 循环即可。

#include <cstdint>
#include <functional>
#include <chrono>
#include <thread>
#include <future>
#include <condition_variable>
#include <iostream>
#include <mutex>

class Ticker {
public:
    typedef std::chrono::duration<int64_t, std::nano> tick_interval_t;
    typedef std::function<void()> on_tick_t;

    Ticker (std::function<void()> onTick, std::chrono::duration<int64_t, std::nano> tickInterval) 
    : _onTick (onTick)
    , _tickInterval (tickInterval)
    , _running (false) {}
    ~Ticker () {}

    void start () {
        if (_running) return;
        _running = true;
        std::thread run(&Ticker::timer_loop, this);
        run.detach();
    }

    void stop () { _running = false; }

    void setDuration(std::chrono::duration<int64_t, std::nano> tickInterval)
    {
        _tickIntervalMutex.lock();
        _tickInterval = tickInterval;
        _tickIntervalMutex.unlock();
    }

private:
    void timer_loop()
    {
        while (_running) {
            std::thread run(_onTick );
            run.detach();

            _tickIntervalMutex.lock();
            std::chrono::duration<int64_t, std::nano> tickInterval = _tickInterval;
            _tickIntervalMutex.unlock();
            std::this_thread::sleep_for( tickInterval );
        }
    }

    on_tick_t           _onTick;
    tick_interval_t     _tickInterval;
    volatile bool       _running;
    std::mutex          _tickIntervalMutex;
};

void tick()
{
    std::cout << "tick\n";
}

void main()
{
    std::chrono::duration<int, std::milli> timer_duration1(1000);
    std::chrono::duration<int, std::milli> timer_duration2(500);
    std::chrono::duration<int> main_wait(5);

    Ticker ticker(std::function<void()>(tick), timer_duration1);
    ticker.start();

    std::this_thread::sleep_for(main_wait);
    ticker.setDuration(timer_duration2);
    std::this_thread::sleep_for(main_wait);
    ticker.stop();
}

关于c++ - 在 C++ 中实现计时器/自动收报机类需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24778929/

相关文章:

android - 检查 CountDownTimer 是否正在运行

c++ - 在 Boost.Log 中过滤非平凡的记录器?

c# - 为什么使用 APM 而不是使用单独的线程?

java - Java 中的条件如何知道要触发哪个线程?

c - Solac 单线程 C 应用程序

c - 使用 Time 获取当前日期的天数

java - Swing 计时器的问题

c++ - "Function"不是 C++ 类型

C++接口(interface)类导致函数调用不明确(Qt)

c# - 将 ATL::CComBSTR 转换为 BSTR*