首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C++ 11创建计时器事件?

如何使用C++ 11创建计时器事件?
EN

Stack Overflow用户
提问于 2013-02-02 00:36:23
回答 6查看 126.3K关注 0票数 52

如何使用C++ 11创建计时器事件?

我需要这样的东西:“从现在起1秒后给我打电话”。

有图书馆吗?

EN

回答 6

Stack Overflow用户

发布于 2013-02-03 02:46:57

做了一个简单的实现,我相信这是你想要实现的。您可以将类later与以下参数一起使用:

  • int (运行代码之前的等待时间为毫秒)
  • bool(如果为真,它会立即返回,并在指定时间过后在另一个thread)
  • variable参数上运行代码(与您提供给std::bind)

的参数完全相同

您可以将std::chrono::milliseconds更改为std::chrono::nanosecondsmicroseconds以获得更高的精度,并添加第二个int和for循环来指定运行代码的次数。

给你,好好享受吧:

代码语言:javascript
复制
#include <functional>
#include <chrono>
#include <future>
#include <cstdio>

class later
{
public:
    template <class callable, class... arguments>
    later(int after, bool async, callable&& f, arguments&&... args)
    {
        std::function<typename std::result_of<callable(arguments...)>::type()> task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));

        if (async)
        {
            std::thread([after, task]() {
                std::this_thread::sleep_for(std::chrono::milliseconds(after));
                task();
            }).detach();
        }
        else
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(after));
            task();
        }
    }

};

void test1(void)
{
    return;
}

void test2(int a)
{
    printf("%i\n", a);
    return;
}

int main()
{
    later later_test1(1000, false, &test1);
    later later_test2(1000, false, &test2, 101);

    return 0;
}

两秒钟后的输出:

代码语言:javascript
复制
101
票数 78
EN

Stack Overflow用户

发布于 2017-10-05 01:59:32

Edward的异步解决方案:

  • 创建新线程并在该线程中休眠
  • 在该线程中执行任务

很简单,而且可能对你有用。

我还想给出一个更高级的版本,它具有以下优点:

  • 无线程启动每个进程需要一个额外的线程来处理所有定时任务

这在大型软件项目中可能特别有用,在这些项目中,您的流程中有许多重复执行的任务,并且您关心资源使用(线程)和启动开销。

想法:有一个服务线程来处理所有注册的定时任务。为此,请使用boost io_service。

代码类似于:http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/tutorial/tuttimer2/src.html

代码语言:javascript
复制
#include <cstdio>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main()
{
  boost::asio::io_service io;

  boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
  t.async_wait([](const boost::system::error_code& /*e*/){
    printf("Printed after 1s\n"); });

  boost::asio::deadline_timer t2(io, boost::posix_time::seconds(1));
  t2.async_wait([](const boost::system::error_code& /*e*/){
    printf("Printed after 1s\n"); });

  // both prints happen at the same time,
  // but only a single thread is used to handle both timed tasks
  // - namely the main thread calling io.run();

  io.run();

  return 0;
}
票数 7
EN

Stack Overflow用户

发布于 2018-10-29 12:52:03

使用RxCpp

代码语言:javascript
复制
std::cout << "Waiting..." << std::endl;
auto values = rxcpp::observable<>::timer<>(std::chrono::seconds(1));
values.subscribe([](int v) {std::cout << "Called after 1s." << std::endl;});
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14650885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档