Use boost thread_pool with boost steady_timer.

huangapple go评论69阅读模式
英文:

Use boost thread_pool with boost steady_timer

问题

以下是翻译好的代码部分:

boost::asio::thread_pool thread_pool{};
boost::asio::steady_timer timer{thread_pool.get_executor(), std::chrono::seconds{1}};

timer.async_wait([](const boost::system::error_code& ec){
     std::cout << "Task Completed" << std::endl;
});

请告诉我如果您需要进一步的信息。

英文:

I want to use execute a timer-based task with a pool of threads. I wrote the following code:

boost::asio::thread_pool thread_pool{};
boost::asio::steady_timer timer{thread_pool.get_executor(), std::chrono::seconds{1}};

timer.async_wait([](const boost::system::error_code&amp;){
     std::cout &lt;&lt; &quot;Task Completed&quot; &lt;&lt; std::endl;
});

Is this the correct way to achieve this? or should I be using an io_context and then run that in every thread of thread_group? From what I can find online is that io_context should be used for any io including timer-related tasks but then why does the above code compile with thread_pool? Since one can argue that using thread_pool provides a cleaner outlook is there any place I should use io_context?

答案1

得分: 1

> 这是实现这个的正确方法吗?

这是一个可以工作的正确方法。

> 或者我应该使用一个 io_context,然后在 thread_group 的每个线程中运行它?

你不应该。基本上是因为这几乎是完全相同的事情,但不太正确。使用自己的线程组时,你需要做一些工作来保持公平调度并正确处理异常。

> 由于有人可以争论使用 thread_pool 提供了更清晰的视角,我应该在哪里使用 io_context 吗?

实际上是一个很好的观点。当你不想要线程时,应该使用 io_context。有时你的平台不支持线程创建,线程调度引入了你想避免的非确定性,或者你只是不想在全局范围内有任何同步问题。例如:

int main() {
   io_context ioc(1);
   start_server(ioc);
   ioc.run(); // 主线程
}

在概念上类似于:

int main() {
   thread_pool ioc(1);
   start_server(ioc);
   ioc.join(); // 单独的线程
}

我通常写后者,也因为它可以减少对工作保护的需求(thread_pool 在内部持有执行器工作保护)。

英文:

> Is this the correct way to achieve this?

It is one correct way that can work

> or should I be using an io_context and then run that in every thread
> of thread_group?

You should not. Basically for the reason that it is almost exactly the same thing, but less correct. With your own thread-groups you have to do work to maintain fair scheduling and handle exceptions correctly.

> Since one can argue that using thread_pool provides a cleaner outlook is there any place I should use io_context?

Good point actually. You should use io_context when you don't want threads. Sometimes your platform doesn't support thread creation, thread scheduling introduces non-determinism you want to avoid, or you just don't want to have any synchronization issues even regarding globals. E.g.

int main() {
   io_context ioc(1);
   start_server(ioc);
   ioc.run(); // main thread
}

Is conceptually similar to

int main() {
   thread_pool ioc(1);
   start_server(ioc);
   ioc.join(); // separate thread
}

I liberally write the latter, also because it can reduce the need for work-guards (thread_pool hold an executor work guard internally).

huangapple
  • 本文由 发表于 2023年7月4日 20:33:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定