C language > Beginner questions

does _sleep(0) have some minimum execution time?

(1/1)

bobgardner:
Can we use _sleep(0) like a short delay in a loop?

jj2007:
You can, but your CPU will run hot. If you use Sleep(1) instead, it will stay cool. Test it...

TimoVJL:
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: ---void __cdecl _sleep(unsigned long dwDuration)
{
    if (dwDuration == 0) {
        dwDuration++;
    }
    Sleep(dwDuration);
}

--- End code ---

EDIT:
this example gives replacement for PellesC.
Define project preprosessor symbols: _sleep=_ms_sleep

--- Code: ---//ms_sleep.c for library
__declspec(dllimport) void __stdcall Sleep(long);
void __cdecl _ms_sleep(unsigned long dwDuration)
{
    if (dwDuration == 0) {
        dwDuration++;
    }
    Sleep(dwDuration);
}
--- End code ---
or
--- Code: ---//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);
}
--- End code ---

--- Code: ---//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;
}
--- End code ---
Sleep(0)

jj2007:
Sometimes a look under the hood is a good idea:
--- Code: ---  _asm int 3;
  Sleep(0);
  _asm nop;
  _asm nop;
  _asm nop;

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

--- End code ---


--- Code: ---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
--- End code ---

Navigation

[0] Message Index

Go to full version