boost deadline_timer最小示例:我应该替换“sleep”吗? [英] boost deadline_timer minimal example: should I substitute "sleep"?

查看:404
本文介绍了boost deadline_timer最小示例:我应该替换“sleep”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程,我需要每10毫秒做一些事情。所以我有非常简单的代码,像这样:

I have a thread where I need to do something every 10 ms. So I have very simple code, like that:

while (work) {
    // do something
    Sleep(10000); // boost sleep can be also used
}

我听说 Sleep 一般不推荐,如果我用 deadline_timer 替换它整体应用程序的性能会更好,特别是我会避免昂贵的上下文切换。

I heard that Sleep is not recommended in general and if I substitute it with deadline_timer overall application performance will be better, in particular I will avoid expensive "context switch".

如果我将 sleep 更改为 deadline_timer

推荐答案

这取决于10ms的要求。

It all depends on the requirements for the 10ms.

如果应用程序在迭代之间需要10ms的延迟,是好的。假设 work()需要7毫秒完成,时间线将导致以下结果:

If an application needs a 10ms delay between iterations, then sleep is fine. Assuming work() takes 7 milliseconds to complete, the timeline would result in the following:

 Time  | Action
-------+------------
0.000s | begin work
0.007s | finish work, block
0.017s | finish blocking, begin work
0.024s | finish work, block
0.034s | finish blocking, begin work

这可能值得考虑使用Boost.Thread的 this_thread :: sleep_for() ,以便于阅读:

It may be worth considering using Boost.Thread's this_thread::sleep_for() for readability:

#include <boost/thread.hpp>

int main()
{
  for (;;)
  {
    work();
    boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
  }
}






如果迭代之间的最大延迟为10ms,则执行工作所花费的时间需要从10ms延迟减少。假设 work()需要7毫秒完成,时间线将导致以下结果:


10ms max delay between iterations

If the max delay between iterations is 10ms, then the time spent executing work needs to be reduced from the 10ms delay. Assuming work() takes 7 milliseconds to complete, the timeline would result in the following:

 Time  | Action
-------+------------
0.000s | begin work
0.007s | finish work, block
0.010s | finish blocking, begin work
0.017s | finish work, block
0.020s | finish blocking, begin work

使用定时器同步教程可以是一个好的开始的地方。需要考虑的一点是Boost.Asio提供了几个计时器。如果10ms延迟不应受到系统时钟更改的影响,请考虑使用 steady_timer 。否则, deadline_timer 应该很好。

The using a timer synchronously tutorial can be a good place to start. One point to consider is that Boost.Asio provides a few timers. If the 10ms delays should not be affected by changes to the system clock, then a consider using steady_timer. Otherwise, deadline_timer should be fine.

#include <boost/asio/steady_timer.hpp>

boost::asio::io_service io_service;
boost::asio::steady_timer timer(io_service);

int main()
{
  for (;;)
  {
    timer.expires_from_now(boost::chrono::milliseconds(10));
    work();
    timer.wait();
  }
}


$ b < work()需要13毫秒来完成,那么在工作之间不会有延迟,因为已经超过了最大延迟。然而,这导致 work()每13毫秒执行一次,而不是每10毫秒执行 work()

Another consideration is that if work() takes 13 milliseconds to complete, then there will be no delay between work, as the max delay has been exceeded. However, this results in work() being done every 13 milliseconds, rather than work() being done every 10 milliseconds.

 Time  | Action
-------+------------
0.000s | begin work
0.013s | finish work, block
0.013s | finish blocking, begin work
0.026s | finish work, block
0.039s | finish blocking, begin work






每10ms执行一次工作



如果完成 work()所需的时间超过延迟,则 work()将不会每10ms做一次。为了实现这一点,可能需要使用多个线程。以下是一个时间线,有两个线程异步执行工作,每10毫秒调度一次,但需要13毫秒完成:


Perform work every 10ms

If the time it takes to complete work() exceeds the delay, then work() will not be done every 10ms. To accomplish this, multiple threads may need to be used. The following is a timeline with 2 threads asynchronously performing work that is scheduled every 10 milliseconds, but takes 13 milliseconds to complete:

 Time  | Thread A                   | Thread B
-------+----------------------------+---------------------------
0.000s | schedule work, begin work  |
0.010s |                            | schedule work, begin work 
0.013s | finish work, block         |
0.020s | schedule work, begin work  |
0.023s |                            | finish work, block
0.030s |                            | schedule work, begin work
0.033s | finish work, block         |

异步使用计时器可以提供一个基本介绍。总体想法是将工作添加到 io_service 中,并且每10毫秒一个运行 io_service 的线程将选择调用 work()。线程池大小可以根据 work()需要完成的时间量来增加或减少。在工作需要7毫秒的情况下,则单个线程可以异步地等待计时器。

The using a timer asynchronously may provide a basic introduction. The overall idea is to add work into the io_service, and every 10 milliseconds a thread that is running the io_service will be selected to invoke work(). The thread pool size can be increased or decreased based on the amount of time work() takes to complete. In the case where work takes 7 milliseconds, then a single thread could asynchronously wait on the timer.

#include <boost/asio/steady_timer.hpp>

boost::asio::io_service io_service;
boost::asio::steady_timer timer(io_service);

void handle_timer(const boost::system::error_code& error);

void schedule_work()
{
  // Schedule more work.
  timer.expires_from_now(boost::chrono::milliseconds(10));
  timer.async_wait(&handle_timer);
}

void handle_timer(const boost::system::error_code& error)
{
  if (error) return;
  schedule_work();
  work();
}

int main()
{
  // Add work to io_service.
  schedule_work();

  // Create 2 threads that will run io_service.
  boost::thread_group threads;
  for (std::size_t i = 0; i < 2; ++i)
    threads.create_thread(boost::bind(
      &boost::asio::io_service::run, &io_service));

  // Wait for threads to finish.
  threads.join_all();
}



当引入并发性以满足截止日期时,请验证 work()是线程安全的。

这篇关于boost deadline_timer最小示例:我应该替换“sleep”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆