Hi,
What's the best way to make less important threads consume less CPU? Searching for the word "thread" isn't very helpful because all forum threads are named "thread".
I'm using boost's threads but they can't be assigned a priority as far as I can see. Should I just sleep them?
Basically, I got a thread that talks on a COM port. The device it talks to is slow and sometimes takes 1-2 seconds to respond. I don't want the application to halt when it talks to the device, but I couldn't care less if the response gets to the user a few seconds later than usual. Would a low priority thread help here? Or sleeping a few seconds each loop?
Do I make sense?
Thanks!
Less important threads (system threading)
Started by SymLinked, Jan 24 2011 01:10 PM
6 replies to this topic
Sponsor:
#2 Members - Reputation: 2049
Posted 24 January 2011 - 01:15 PM
Disclaimer: I know hardly anything about COM ports.
Can you open it as a file and use WaitForSingleObject (or even overlapped I/O) on Windows, or select() on other operating systems?
Aside from COM ports, manually changing thread priority is usually frowned upon.
Can you open it as a file and use WaitForSingleObject (or even overlapped I/O) on Windows, or select() on other operating systems?
Aside from COM ports, manually changing thread priority is usually frowned upon.
#3 Moderators - Reputation: 8424
Posted 24 January 2011 - 01:47 PM
If IO is already on a separate thread, why would slow IO halt the main application interface?
Maker of Machinery
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#4 Members - Reputation: 2369
Posted 24 January 2011 - 01:50 PM
No.Would a low priority thread help here?
Each read takes X amount of time. To perform n reads, the CPU time is X*n.
This is the CPU time required to do useful work. It is not possible to receive n bytes in shorter time.
While the thread is blocking on IO, it's idle.
COM ports transfer 115kbits at best, or around 15 kilobytes per second. If reading at maximum capacity, CPU will be idle for 99.9999% of the time, quite literally.
#5 Members - Reputation: 478
Posted 24 January 2011 - 02:19 PM
I miswrote, sorry about that! The code wasn't in a thread, which was why it blocked. Each batch takes 4 seconds to complete. I wanted to put it inside a thread and have done so successfully. But...
99% of the time there are no messages to be sent to the device - in which case it's going to be wasteful to loop inside the thread, looking for more messages to send.
The thread is set at normal priority and I want to set it lower to avoid wasting cycles. Is this the right approach or should I just sleep for a few seconds each loop? Would this lower the overhead?
Thanks for the replies and sorry about the confusion.
99% of the time there are no messages to be sent to the device - in which case it's going to be wasteful to loop inside the thread, looking for more messages to send.
The thread is set at normal priority and I want to set it lower to avoid wasting cycles. Is this the right approach or should I just sleep for a few seconds each loop? Would this lower the overhead?
Thanks for the replies and sorry about the confusion.
#6 Members - Reputation: 313
Posted 24 January 2011 - 02:52 PM
In multithreaded code, sleeping for a time interval is almost never the right thing to do. If your code is waiting for something to happen, wait for that event.
In your case, it sounds like your thread should sleep until either (a) data is available on the COM port, or (b) some other part of your application has queued a message to be sent. You can use WaitForMultipleObjects to wait for one of those conditions to hold.
In your case, it sounds like your thread should sleep until either (a) data is available on the COM port, or (b) some other part of your application has queued a message to be sent. You can use WaitForMultipleObjects to wait for one of those conditions to hold.
#7 Members - Reputation: 2369
Posted 24 January 2011 - 02:53 PM
it's going to be wasteful to loop inside the thread
A thread running a spin loop will be running to fill any idle resources regardless of priority. The whole point of priority is to allow higher priority threads to run, not to save cycles. It just determines the order in which threads are scheduled.
This is solved using a condition variable, it's probably the most basic of all thread communication examples. This, for example.






