In the main loop of a worker thread that has to poll to know if there's work to do, I let it sleep in order to not busy-wait. How do I determine a good length for sleeping? E. g. if I wake up every millisecond, it's probably useless to sleep at all. If I wake up every ten minutes, it might make the app look unresponsive (depending on what the thread is doing).
I've heard before what cadrian says below, that a human user won't notice timespans around 100 ms, but what about approaching it from the machine side? How small can the interval get before it starts to get wasteful?
I guess, it boils down to a more general (i. e. platform-independent) version of this question.
EDIT: Of course, the question should always be rephrased first as "How can I change this to the event pattern instead of polling", but let's assume for now that I can't or don't want to.
Sometimes polling is the answer.
However, how often it polls depends on what the thread is doing, so make it configurable.
We have threads that check for email. They can check external mail boxes, we only check the mailboxes every thirty seconds or so (but it's configurable). Each check is a call over the web, calling every second or 10th of a second would hammer the network, 30 seconds is ok, and it doesn't matter if the email isn't processed THE MOMENT it lands in the in box (however some customer sites check every 5 mintues).
We have other threads that poll for files in a folder, and/or for new requests added to a table in the database. These poll for work every 1 to 5 seconds (depending on what they're doing).
Now regardless of how long you wait between polling, we don't let any of our threads sleep for longer than a second. The reason being, if you try to stop a windows service that has a thread sleeping for 60 seconds, you could be waiting 60 seconds before the service stops.
If you need to bring a machine down in a hurry, a Rip Van Winkle thread will really twist your mellon.
So, have your polling intervals as far apart as you need, but don't sleep for much longer than a second.
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With