Forum > General
[Solved] Service & threads
afortuny:
Hi folks.
I'm developing a service (Win & Linux) which launches some threads to do the job. So far, so good.
When all threads are running, the machine CPU is used at almost 100% and all other running programs and processes are slowed down in a severe way.
Is it any kind of code I could add to my service which will give the OS a chance to allow time slices to other processes besides the classical Sleep() ? I think basically to the other classic "Application.processMessages" which cannot be used in a service neither into an independent thread.
Antonio.
mas steindorff:
I think what your after is some way to set the threads priority down to the lowest level so that it can run all of the time (sorry I don't know how to do this but now you have a keyword for searching)
you seem to know this already, but for the others viewing... a sleep(1) will release the thread for a very short time and then continues if nothing needs the CPU. I've use that as my "poor mans" priority control that work just fine for what I need it for.
afortuny:
Hi all.
Got a clean solution on my own.
Instead of using the Sleep() function, I've replaced it by a little object using a TTimer and a TEvent. The TTimer.OnTiimer sets the event whilst a call to the TMySleep.Sleep(ms: integer) sets the timer delay (ms) and resets the TEvent. The function then calls TMySleep.WaityFor(ms+100). Because it is implemented as an object created inside every thread with a different name there is no problem.
As a matter of result, it looks very nice. With the previous code using the classical Sleep() the PC was loaded almost up to 100%. Replacing exaclty on the same places, unloads the cpu (dual core) down to less than 40% with only peaks showing instead of a continous curve around 100%. The "sleeping" times go between 1 and 3 seconds.
If you are interested to know about the code let me know.
Antonio.
CaptBill:
Hi afortuny,
Have you tried setting the Tthreadpriority property of the thread variable?
Found a article/tutorial that calls it like this:
--- Code: ---ct:= tCountthread.create;
ct.Priority:= TThreadPriority( [x] );
--- End code ---
I think x is a enumerated stringlist with values [idle,lowest,lower,normal,higher,highest,TimeCritical]
so try :
--- Code: ---ct.Priority:= TThreadpriority(lowest);
--- End code ---
JD:
--- Quote from: afortuny on September 12, 2011, 04:31:42 pm ---If you are interested to know about the code let me know.
Antonio.
--- End quote ---
I am interested. I have a similar problem on one of my projects. Thanks for the tips.
JD
Navigation
[0] Message Index
[#] Next page