You will find several threads during my application that actually work without anyone's knowledge. They connect with database and execute a while consuming choose queries. In many of cases these queries return only several records. Every so often, however, they might return hundreds of 1000's records. These types of then processed inside a loop.

Because such situation may exist in several threads simultaneously, I don't want my application to make use of 100% of CPU time when individuals threads are processing data neither do I wish to make all threads fight for processor's time. Well, I call the Sleep() function in every iteration from the loops in individuals threads.

I don't know, however, how you can adjust the sleep time. I don't want the loops to last forever, therefore the sleep period cannot be too lengthy. I place it to two milliseconds in every iteration (in every thread) (why 2ms? - this is a good question :) ).

I figured, however, which i could prolong the sleep time, but call sleep only one time every n iterations (let us say, Sleep(100) every 50 iterations). Which approach must i choose? One itertion from the loops takes about 30 ms each (with no sleeps).

Please advise.


Calling Sleep() will not make any sense. Make use of all the processing energy you will get, and allow the system see how to best schedule your threads. Calling Sleep() is only going to cause extra context switches minimizing your throughput. If background processing intervenes together with your primary thread or any other programs, then lower the priority of background threads accordingly.

If you wish to let threads sleep to limit the quantity of data they generate for customers, consider producer-consumer-queues. Possess the producer threads simply block when their queue is full, by doing this you won't need whatsoever to fiddle using the timing.

Note additionally that using maximum CPU generally is a positive thing, especially on modern processors. Even on laptops getting a short while of high load is preferable to unnaturally extending time your career needs, because the processor / whole system will sooner have the ability to enter lower energy states.

There might be better approaches than using sleep to manage your threads. As your database call can return 1-a large number of records for processing, it could seem sensible to split up the application into two layers, maybe utilizing a message queue to buffer the demands. Your application could call off and away to an information service, and also the data service would then run the query and dispatch individual data messages (or blocks of messages, etc.) to some queue. Your application may then create as numerous threads as appropriate to process messages. More threads means faster processing at the fee for CPU, however, you may change this to obtain the right balance.

You best simply make low priority threads and allow them to work without sleeps, otherwise you don't make use of the full energy from the CPU. For instance on multi-core/multi-cpu systems. Or maybe the body is 100% idle: why wait or sleep?

But when you'll need some sleeps however, observe that sleep(1) waits 10-15ms (!) due to the default Home windows timeslice. You should use timeBeginPeriod(1) from the MMSystem.pas unit to create the resolution to 1ms :-) (I made use of this for serial communication).