<legend id='rhsJ5'><style id='rhsJ5'><dir id='rhsJ5'><q id='rhsJ5'></q></dir></style></legend>

      <bdo id='rhsJ5'></bdo><ul id='rhsJ5'></ul>

      <i id='rhsJ5'><tr id='rhsJ5'><dt id='rhsJ5'><q id='rhsJ5'><span id='rhsJ5'><b id='rhsJ5'><form id='rhsJ5'><ins id='rhsJ5'></ins><ul id='rhsJ5'></ul><sub id='rhsJ5'></sub></form><legend id='rhsJ5'></legend><bdo id='rhsJ5'><pre id='rhsJ5'><center id='rhsJ5'></center></pre></bdo></b><th id='rhsJ5'></th></span></q></dt></tr></i><div id='rhsJ5'><tfoot id='rhsJ5'></tfoot><dl id='rhsJ5'><fieldset id='rhsJ5'></fieldset></dl></div>
    1. <tfoot id='rhsJ5'></tfoot>
    2. <small id='rhsJ5'></small><noframes id='rhsJ5'>

    3. 需要在 C++ 中以周期性的时间间隔调用一个函数

      时间:2023-06-03

      <legend id='DVxPY'><style id='DVxPY'><dir id='DVxPY'><q id='DVxPY'></q></dir></style></legend>

                <bdo id='DVxPY'></bdo><ul id='DVxPY'></ul>
              • <tfoot id='DVxPY'></tfoot>
              • <i id='DVxPY'><tr id='DVxPY'><dt id='DVxPY'><q id='DVxPY'><span id='DVxPY'><b id='DVxPY'><form id='DVxPY'><ins id='DVxPY'></ins><ul id='DVxPY'></ul><sub id='DVxPY'></sub></form><legend id='DVxPY'></legend><bdo id='DVxPY'><pre id='DVxPY'><center id='DVxPY'></center></pre></bdo></b><th id='DVxPY'></th></span></q></dt></tr></i><div id='DVxPY'><tfoot id='DVxPY'></tfoot><dl id='DVxPY'><fieldset id='DVxPY'></fieldset></dl></div>

              • <small id='DVxPY'></small><noframes id='DVxPY'>

                  <tbody id='DVxPY'></tbody>
                本文介绍了需要在 C++ 中以周期性的时间间隔调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在用 C++ 编写一个程序,我需要以周期性的时间间隔调用一个函数,比如每 10 毫秒左右.我从未在 C++ 中做过与时间或时钟相关的任何事情,这是一个快速简便的问题,还是没有巧妙解决方案的问题?

                I am writing a program in c++ where I need to call a function at periodic time intervals, say every 10ms or so. I've never done anything related to time or clocks in c++, is this a quick and easy problem or one of those where there is no neat solution?

                谢谢!

                推荐答案

                为了完成这个问题,@user534498 的代码可以很容易地调整为具有周期性的滴答间隔.只需要在定时器线程循环开始时和sleep_until 执行函数后确定下一个开始时间点.

                To complete the question, the code from @user534498 can be easily adapted to have the periodic tick interval. It's just needed to determinate the next start time point at the beginning of the timer thread loop and sleep_until that time point after executing the function.

                #include <iostream>
                #include <chrono>
                #include <thread>
                #include <functional>
                
                void timer_start(std::function<void(void)> func, unsigned int interval)
                {
                  std::thread([func, interval]()
                  { 
                    while (true)
                    { 
                      auto x = std::chrono::steady_clock::now() + std::chrono::milliseconds(interval);
                      func();
                      std::this_thread::sleep_until(x);
                    }
                  }).detach();
                }
                
                void do_something()
                {
                  std::cout << "I am doing something" << std::endl;
                }
                
                int main()
                {
                  timer_start(do_something, 1000);
                  while (true)
                    ;
                }
                

                这篇关于需要在 C++ 中以周期性的时间间隔调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:打印 C++ 结构中所有字段的值 下一篇:在多线程环境中是否需要保护对 STL 容器的读取访问?

                相关文章

                <tfoot id='4Rmly'></tfoot>

                1. <small id='4Rmly'></small><noframes id='4Rmly'>

                    <bdo id='4Rmly'></bdo><ul id='4Rmly'></ul>

                    <legend id='4Rmly'><style id='4Rmly'><dir id='4Rmly'><q id='4Rmly'></q></dir></style></legend>
                    <i id='4Rmly'><tr id='4Rmly'><dt id='4Rmly'><q id='4Rmly'><span id='4Rmly'><b id='4Rmly'><form id='4Rmly'><ins id='4Rmly'></ins><ul id='4Rmly'></ul><sub id='4Rmly'></sub></form><legend id='4Rmly'></legend><bdo id='4Rmly'><pre id='4Rmly'><center id='4Rmly'></center></pre></bdo></b><th id='4Rmly'></th></span></q></dt></tr></i><div id='4Rmly'><tfoot id='4Rmly'></tfoot><dl id='4Rmly'><fieldset id='4Rmly'></fieldset></dl></div>