What happens when Delay() is processed

Just starting out? Need help? Post your questions and find answers here.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

What happens when Delay() is processed

Post by Nubcake »

This was meant to be a general question independent of language but never the less I chose to post it here: I've always wandered what does the CPU do when a program reaches a line that contains Delay() or in general a command that sleeps the current thread it is on. For example if I wrote an infinite while loop in the main thread and executed the program it would eat all the CPU time for that particular core it is running on (25% of the processor for me) . However if I added a Delay(50) , what does the CPU do during those 50ms ? Does it know that in the current thread that it has found this delay it should give time back to the OS to do other things or start processing another thread instead ? Is this why some code examples contain Delay() commands in them?

Thanks
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: What happens when Delay() is processed

Post by Danilo »

Delay() is not a CPU/ASM command, it is an OS function. It tells the OS that it should not switch to your
thread for a certain amount of time. The OS Task Scheduler switches to other processes in the meantime.
When the OS itself has nothing to do, it can tell the CPU so, for example with HLT instruction, to enter Idle state.
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 210
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: What happens when Delay() is processed

Post by Otrebor »

Danilo wrote:Delay() is not a CPU/ASM command, it is an OS function. It tells the OS that it should not switch to your
thread for a certain amount of time. The OS Task Scheduler switches to other processes in the meantime.
When the OS itself has nothing to do, it can tell the CPU so, for example with HLT instruction, to enter Idle state.
Interesting!
thank's :)
User avatar
Teddy Rogers
User
User
Posts: 98
Joined: Sun Feb 23, 2014 2:05 am
Location: Australia
Contact:

Re: What happens when Delay() is processed

Post by Teddy Rogers »

Delay is a rebadged version of the Windows API Sleep function...

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Ted.
Post Reply