Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - adjusting thread sleep time

There are several threads in my application that work in the background. They connect to database and execute some time consuming select queries. In most of cases these queries return only several records. From time to time, however, they may return tens of thousands records. All these are then processed in a loop.

Because such situation may occur in several threads at the same time, I do not want my application to use 100% of CPU time when those threads are processing data; neither do I want to make all threads fight for processor's time. Therefore I call the Sleep() function in each iteration of the loops in those threads.

I do not know, however, how to adjust the sleep time. I do not want the loops to last forever, so the sleep period can not be too long. I set it to 2 milliseconds in each iteration (in each thread) (why 2ms? - that's a good question :) ).

I thought, on the other hand, that I could prolong the sleep time, but call sleep only once every n iterations (let's say, Sleep(100) every 50 iterations). Which approach should I choose? One itertion of the loops takes about 30 ms each (without any sleeps).

Please advise.

Thanks!
Mariusz.

like image 544
Mariusz Schimke Avatar asked Jun 06 '09 21:06

Mariusz Schimke


1 Answers

Calling Sleep() makes no sense. Use all the processing power you can get, and let the system work out how to best schedule your threads. Calling Sleep() will only cause extra context switches and lower your throughput. If background processing interferes with your main thread or other applications, then lower the priority of background threads accordingly.

If you want to let threads sleep to limit the amount of data they generate for consumers, look into producer-consumer-queues. Have the producer threads simply block when their queue is full, this way you will not need at all to fiddle with the timing.

Note also that using maximum CPU is generally a good thing, especially on modern processors. Even on laptops having a short time of high load is better than artificially prolonging the time your task needs, as the processor / whole system will sooner be able to enter lower power states.

like image 77
mghie Avatar answered Oct 31 '22 05:10

mghie