开发者

Performance/priority in Boost thread with accept() (Linux)

开发者 https://www.devze.com 2023-03-20 21:04 出处:网络
I have implemented a C++ class which launches a separate thread accepting connections (with boost::asio). The whole class is intended to b开发者_运维百科e an asynchronous \"command receiver\" for a bi

I have implemented a C++ class which launches a separate thread accepting connections (with boost::asio). The whole class is intended to b开发者_运维百科e an asynchronous "command receiver" for a bigger program.

The main trouble is: when waiting for incoming connections, the thread is substantially idle. My perception is that this is wasting processor time (and hardware thread) just doing nothing. Is there any way to execute the thread with a lower priority, or some other way to make the thread not to interfere with the main program, which is both task intensive and threaded?


if you or the class you use is doing an accept call, then the thread is blocked and is waiting for the OS to do some things. SO the thread is (should) not be consuming any CPU ressources, if it does so, it is either

  • within the system call (nothing you could change) or
  • there is a time-out and the thread is looping to accept again. If there is any parameter to change this, try it!


Assuming you are using the boost thread class you can change thread priorities by using the native_handle() handle function in boost thread. Here is a small example.

But first I would suggest you verify that you really have a problem. The thread you are concerned about should be effectively idle anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消