<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++ 中以周期性的时间间隔调用一个函数

      need to call a function at periodic time intervals in c++(需要在 C++ 中以周期性的时间间隔调用一个函数)

      <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++ 中以周期性的时间间隔调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                  <tfoot id='4Rmly'></tfoot>

                      <tbody id='4Rmly'></tbody>
                    • <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>