2

I have implemented a C++ class which launches a separate thread accepting connections (with boost::asio). The whole class is intended to be 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?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Carlo Bono
  • 95
  • 1
  • 8

2 Answers2

3

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!
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36
  • i think you're right. i can't get any evidece with ps that the threas is consuming any resources. thank you! – Carlo Bono Jul 18 '11 at 15:53
1

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.

Community
  • 1
  • 1
Duck
  • 26,924
  • 5
  • 64
  • 92
  • same as the other comment of mine :D thank you, the code you suggested will be useful for other purposes. +1 ;) – Carlo Bono Jul 18 '11 at 15:55