Recent

Author Topic: [Solved] Service & threads  (Read 6183 times)

afortuny

  • New Member
  • *
  • Posts: 21
[Solved] Service & threads
« on: September 08, 2011, 02:16:53 pm »
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.
« Last Edit: September 12, 2011, 04:34:00 pm by afortuny »

mas steindorff

  • Hero Member
  • *****
  • Posts: 555
Re: Service & threads
« Reply #1 on: September 08, 2011, 08:59:08 pm »
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.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

afortuny

  • New Member
  • *
  • Posts: 21
Re: Service & threads
« Reply #2 on: September 12, 2011, 04:31:42 pm »
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

  • Sr. Member
  • ****
  • Posts: 435
Re: [Solved] Service & threads
« Reply #3 on: September 12, 2011, 07:19:39 pm »
Hi afortuny,

Have you tried setting the Tthreadpriority property of the thread variable?
Found a article/tutorial that calls it like this:

Code: [Select]
ct:= tCountthread.create;
ct.Priority:= TThreadPriority( [x] );

I think x is a enumerated stringlist with values [idle,lowest,lower,normal,higher,highest,TimeCritical]

so try :

Code: [Select]
ct.Priority:= TThreadpriority(lowest);


« Last Edit: September 12, 2011, 07:21:47 pm by CaptBill »

JD

  • Hero Member
  • *****
  • Posts: 1906
Re: Service & threads
« Reply #4 on: September 13, 2011, 09:58:02 am »
If you are interested to know about the code let me know.

Antonio.

I am interested. I have a similar problem on one of my projects. Thanks for the tips.

JD
Linux Mint - Lazarus 4.0/FPC 3.2.2,
Windows - Lazarus 4.0/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

afortuny

  • New Member
  • *
  • Posts: 21
Re: [Solved] Service & threads
« Reply #5 on: September 13, 2011, 09:59:08 am »
Hi CaptBill

In fact it was the first trick I tried but I forgot about it because a lack of flexibility. My last implementation let's me alllow fine time slices depending on what the thread is actually doing.

Anyway, I have implemented the priority as well giving the thread the ability to read its working parameters from time to time. This will help too.

Antonio.

afortuny

  • New Member
  • *
  • Posts: 21
Re: [Solved] Service & threads
« Reply #6 on: September 13, 2011, 10:21:52 am »
Hi JD.

No problem, see attachment. One very tiny unit almost self explanatory.  8-)
Feel free to contact me in case of troublrs.  :)

Antonio.

afortuny

  • New Member
  • *
  • Posts: 21
Re: [Solved] Service & threads
« Reply #7 on: September 13, 2011, 10:28:00 am »
Sorry JD, forgot the source  %)

Here it is.

Antonio

JD

  • Hero Member
  • *****
  • Posts: 1906
Re: [Solved] Service & threads
« Reply #8 on: September 13, 2011, 10:35:13 am »
Sorry JD, forgot the source  %)

Here it is.

Antonio

Thanks a lot, Antonio. The code is clean and easy to understand. I'll try it.
Linux Mint - Lazarus 4.0/FPC 3.2.2,
Windows - Lazarus 4.0/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

 

TinyPortal © 2005-2018