gpt4 book ai didi

c++ - 为进程设置更高的优先级到 boost::asio 线程

转载 作者:行者123 更新时间:2023-12-01 14:25:13 25 4
gpt4 key购买 nike

我知道 C++ 有一些方法可以设置线程的优先级。像正在讨论的东西here .与此类似,boost::asio 运行的线程中是否有类似的东西可用?我正在做 async_readasync_write从网络读取和写入数据。

请注意,我确实想像描述的那样在读写之间设置优先级 here in boost::asio example .我宁愿在整个过程中为 asio 阅读器线程设置更高的优先级。

如果 boost::asio 没有提供直接机制来提高其线程的优先级,那么我可能会创建 std::thread,像讨论的那样设置更高的优先级 here并将其提供给 asio 以将其用于 async_read/async_write?或者这可能是一条线吗?

环境:
我正在使用带有 C++11 编译器的 boost 1.68。

最佳答案

我和评论者一起怀疑这是否有用。

但要回答您的问题:不存在 Boost Asio 线程这样的东西。

明确记录了原因:

Threads And Boost Asio

Consequently, it is the library user's responsibility to create and manage all threads to which the notifications will be delivered.

The reasons for this approach include:

  • By only calling io_context::run() from a single thread, the user's code can avoid the development complexity associated with synchronisation. For example, a library user can implement scalable servers that are single-threaded (from the user's point of view).
  • A library user may need to perform initialisation in a thread shortly after the thread starts and before any other application code is executed. For example, users of Microsoft's COM must call CoInitializeEx before any other COM operations can be called from that thread.
  • The library interface is decoupled from interfaces for thread creation and management, and permits implementations on platforms where threads are not available.

因此,您可以创建自己的线程 - 我想主线程除外 - 并且可以按照您需要的任何方式创建。

关于c++ - 为进程设置更高的优先级到 boost::asio 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62452153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com