NO

Author Topic: does _sleep(0) have some minimum execution time?  (Read 79 times)

Offline bobgardner

  • Member
  • *
  • Posts: 1
does _sleep(0) have some minimum execution time?
« on: June 15, 2017, 09:22:24 PM »
Can we use _sleep(0) like a short delay in a loop?

Offline jj2007

  • Member
  • *
  • Posts: 441
Re: does _sleep(0) have some minimum execution time?
« Reply #1 on: June 16, 2017, 01:37:34 AM »
You can, but your CPU will run hot. If you use Sleep(1) instead, it will stay cool. Test it...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1554
Re: does _sleep(0) have some minimum execution time?
« Reply #2 on: June 16, 2017, 08:32:03 AM »
PellesC and MS _sleep() functions are not compatible.

PellesC _sleep() is POSIX style, delay in seconds.
msvcrt _sleep() is wrapper for API Sleep() function and with value 0 it always add 1 before calling Sleep() function.
Code: [Select]
void __cdecl _sleep(unsigned long dwDuration)
{
    if (dwDuration == 0) {
        dwDuration++;
    }
    Sleep(dwDuration);
}

EDIT:
this example gives replacement for PellesC.
Define project preprosessor symbols: _sleep=_ms_sleep
Code: [Select]
//ms_sleep.c for library
__declspec(dllimport) void __stdcall Sleep(long);
void __cdecl _ms_sleep(unsigned long dwDuration)
{
    if (dwDuration == 0) {
        dwDuration++;
    }
    Sleep(dwDuration);
}
or
Code: [Select]
//ms_sleep.c for library
__declspec(dllimport) long __stdcall SleepEx(long,long);
void __cdecl _ms_sleep(unsigned long dwDuration)
{
    if (dwDuration == 0) {
        dwDuration++;
    }
    SleepEx(dwDuration,0);
}
Code: [Select]
//void __cdecl __sleep(unsigned long dwDuration);
//#define _sleep _ms_sleep
//#pragma comment(lib, "ms_sleep.lib")
int main(int argc, char **argv)
{
_sleep(0);
return 0;
}
Sleep(0)
« Last Edit: June 16, 2017, 01:58:59 PM by TimoVJL »
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 441
Re: does _sleep(0) have some minimum execution time?
« Reply #3 on: June 16, 2017, 12:23:07 PM »
Sometimes a look under the hood is a good idea:
Code: [Select]
  _asm int 3;
  Sleep(0);
  _asm nop;
  _asm nop;
  _asm nop;

  _asm int 3;
  _sleep(0);
  _asm nop;
  _asm nop;
  _asm nop;

Code: [Select]
CPU Disasm
Address           Hex dump               Command                            Comments
00401003          ³.  CD 03              int 3
00401005          ³.  6A 00              push 0                             ; ÚTime = 0
00401007          ³.  FF15 D8504000      call near [<&KERNEL32.Sleep>]      ; ÀKERNEL32.Sleep
0040100D          ³.  90                 nop
0040100E          ³.  90                 nop
0040100F          ³.  90                 nop
00401010          ³.  CD 03              int 3
00401012          ³.  6A 00              push 0                             ; ÚArg1 = 0
00401014          ³.  E8 27000000        call 00401040                      ; ÀC_HelloWorldCon.00401040
00401019          ³.  83C4 04            add esp, 4
...
CPU Disasm
Address           Hex dump               Command                            Comments
00401040          Ú$  8B4424 04          mov eax, [esp+4]                   ; C_HelloWorldCon.00401040(guessed Arg1)
00401044          ³.  BA FFFF0000        mov edx, 0FFFF
00401049          ³.  3D FFFF0000        cmp eax, 0FFFF
0040104E          ³.  0F47C2             cmova eax, edx
00401051          ³.  69C0 E8030000      imul eax, eax, 3E8
00401057          ³.  50                 push eax                           ; ÚTime  ***** null! *****
00401058          ³.  FF15 D8504000      call near [<&KERNEL32.Sleep>]      ; ÀKERNEL32.Sleep
0040105E          ³.  31C0               xor eax, eax
00401060          À.  C3                 retn