Hi.
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.