Recent

Author Topic: Sleep(1) in thread's loop can reduce CPU usage much  (Read 2962 times)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7446
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #15 on: May 18, 2019, 02:43:06 pm »
Note that comparing Windows versions that are not the same bittiness and not the same processor (and preferably also mobo chipset) is pointless. The same functions might use other implementations on different hardware, as I already said.

There is an version factor in that only newer versions might use newer hardware however.

furious programming

  • Sr. Member
  • ****
  • Posts: 354
  • I click a little.
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #16 on: May 18, 2019, 02:47:26 pm »
If we designing the software, we do it in the way that it will work perfect on all supported systems (both bitness in the case of 32-bit application), on all processors and all hardware. If these differences are not taken into account, the software will be crappy.
Lazarus 2.0.4 with FPC 3.0.4, Windows XP (all 32-bit)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7446
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #17 on: May 18, 2019, 02:56:58 pm »

My point is that listing differences between "windows versions" without listing bittiness or the hardware (like processor generation) they run on is pointless

kinlion

  • New Member
  • *
  • Posts: 45
  • I Love Lazarus
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #18 on: May 20, 2019, 03:22:28 am »
Learned a lot. Thanks to all  :)
Lazarus 1.8.4 / FPC 3.0.4 / SVN 57972
On Win10 X64, have to compile for Win32

bylaardt

  • Sr. Member
  • ****
  • Posts: 303
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #19 on: May 20, 2019, 03:45:32 am »
My point was that it does not freeze the program/thread,
why sleep?

can't you just use this code instead?
Code: Pascal  [Select]
  1. Application.ProcessMessages;

Cyrax

  • Hero Member
  • *****
  • Posts: 755
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #20 on: May 20, 2019, 11:39:10 am »
My point was that it does not freeze the program/thread,
why sleep?

can't you just use this code instead?
Code: Pascal  [Select]
  1. Application.ProcessMessages;

Nope, you can't use Application.ProcessMessages from a thread. There will be a deadlock (or other disaster) if you call it in a context of thread.

Thaddy

  • Hero Member
  • *****
  • Posts: 8927
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #21 on: May 20, 2019, 11:46:49 am »
Also note Sleep(0) means "relinquish time slice" whereas sleep(1) will do an actual stall. So Sleep(0) is what should be done....
This is weird, but true... Sleep(0) is a special case.... for threading...Sleep(1) is an actual sleep that causes this problem.
See: https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-sleep which is somewhat obscure and needs a good read....
https://stackoverflow.com/questions/3727420/significance-of-sleep0#3727460
« Last Edit: May 20, 2019, 11:52:35 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

440bx

  • Hero Member
  • *****
  • Posts: 1129
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #22 on: May 20, 2019, 11:55:52 am »
Also note Sleep(0) means "relinquish time slice"
That's what it's _supposed_ to mean. 

So Sleep(0) is what should be done....
That's what people who believe the documentation think.   

This is weird, but true... Sleep(0) is a special case.... for threading...
You got that right.  It is a _very_ special case.

The API documentation is quite often what Microsoft wants you to believe, which is almost just as often, quite different from reality.
« Last Edit: May 20, 2019, 12:18:44 pm by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

furious programming

  • Sr. Member
  • ****
  • Posts: 354
  • I click a little.
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #23 on: May 20, 2019, 12:30:19 pm »
why sleep?

can't you just use this code instead?
Code: Pascal  [Select]
  1. Application.ProcessMessages;

No, I can't. The Sleep procedure is used to freeze the operation of the program for a specified time, so that the process or single thread does not consume the processor's power. The ProcessMessages method is used to process messages that are in the queue. As you can see, these are two different things.

Even if we could safely call this method in the side thread, it does not make sense, because in no way will it freeze the thread operation and, at the same time, save the processor's power.

I wrote about the operation of the Sleep procedure because OP was ”surprised” that calling it with a value of 0 causes a power consumption jump of up to 25% (i.e. 100% of one processor core that executes the thread code), and a call with a value of 1 does not.

However, I wrote about the accuracy of this procedure, because on practically every computer, for the same value of 1, the delay will be different and depend of the default system time period. So if someone uses this procedure with the idea of creating relatively precise delays, he must take into account the mentioned imperfection.


That's what it's _supposed_ to mean.

It's not as easy as it seems. What else to understand what these words mean, and what else to know what they actually mean in practice. And the documentation does not explain this in any way.
« Last Edit: May 20, 2019, 12:37:32 pm by furious programming »
Lazarus 2.0.4 with FPC 3.0.4, Windows XP (all 32-bit)

bylaardt

  • Sr. Member
  • ****
  • Posts: 303
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #24 on: May 20, 2019, 03:37:57 pm »
Nope, you can't use Application.ProcessMessages from a thread. There will be a deadlock (or other disaster) if you call it in a context of thread.
I've always used application.processmessages on synchronize.
What's so bad about it?

furious programming

  • Sr. Member
  • ****
  • Posts: 354
  • I click a little.
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #25 on: May 20, 2019, 05:01:40 pm »
I've always used application.processmessages on synchronize.

Okay, but what is the purpose of calling this method?
Lazarus 2.0.4 with FPC 3.0.4, Windows XP (all 32-bit)

bylaardt

  • Sr. Member
  • ****
  • Posts: 303
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #26 on: May 20, 2019, 06:48:46 pm »
Okay, but what is the purpose of calling this method?
to prevent core freezing.
I can improve the control of multiple parallel processes with a higher time response.
When the pipe has no system events and the data is asynchronous, waiting one millisecond for a response may be the worst solution.
PS: It's just part of the brainstorm, not a ultimate solution

440bx

  • Hero Member
  • *****
  • Posts: 1129
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #27 on: May 20, 2019, 10:14:40 pm »
That's what it's _supposed_ to mean.

It's not as easy as it seems. What else to understand what these words mean, and what else to know what they actually mean in practice.
It's not really hard either.  A few small, well chosen, trivial test programs are very revealing about Sleep(0) and the Windows scheduler.

And the documentation does not explain this in any way.
That is true.  There are plenty of APIs to which that comment applies to some degree.  Another example of one like that, a very simple one that comes to mind at this time, is GetWindowModuleFileName https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowmodulefilenamea.  What the documentation leads you to believe about what it does and, what it actually does, are two different things.  That said it occasionally does what the documentation says. <chuckle>


using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

BrunoK

  • Full Member
  • ***
  • Posts: 181
  • Retired programmer
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #28 on: May 21, 2019, 11:33:05 am »
I've always used application.processmessages on synchronize.

Okay, but what is the purpose of calling this method?
Maybe give the application a chance to process OnIdle "messages" since the application thread may be suspended due to an absence of  OS message activity ?
Lazarus trunk r. 59978/03.01.2019 (+/- patches regarding enabled, TScrollBar, TCursorImage). FPC 3.0.4 32 bits. (+heaptrc with leaked ClassName+Revisited TList) , Windows 10 Pro x64 (v. 1903)

User137

  • Hero Member
  • *****
  • Posts: 1790
    • Nxpascal home
Re: Sleep(1) in thread's loop can reduce CPU usage much
« Reply #29 on: May 21, 2019, 05:08:37 pm »
Sleep(1) is a nice thing indeed. It's the core of cpu saving in nxPascal game engine too. This is old (but very functional still) code from 2013:
https://github.com/Zaflis/nxpascal/blob/master/src/nxGame.pas#L147
Smooth 60 FPS animation still should always include it, regardless of its inaccuracy. Players won't see it, other than in the silentless of their cpu coolers.

But in principle if the OS causes sleep to take too long it processes 2 events in a row without sleeping between.
« Last Edit: May 21, 2019, 05:13:58 pm by User137 »