Sleep() vs _sleep() functions

13,818

As RbMm mentioned, _sleep had been implemented as a very thin wrapper around Sleep:

void __cdecl _sleep(unsigned long dwDuration)
{

    if (dwDuration == 0) {
        dwDuration++;
    }
    Sleep(dwDuration);

}

To confirm, we can test it. Fortunately it's easy to test:

#include <iostream>
#include <chrono>
#include <windows.h>
#include <stdlib.h>

using namespace std::chrono_literals;

int main() {
    auto tm1 = std::chrono::system_clock::now();
    _sleep(250);
    auto tm2 = std::chrono::system_clock::now();
    Sleep(250);
    auto tm3 = std::chrono::system_clock::now();
    std::cout << "_sleep took " << (tm2-tm1)/1ms << " ms, Sleep took " << (tm3-tm2)/1ms << " ms\n";
}

Output:

_sleep took 250 ms, Sleep took 250 ms

So it appears both _sleep and Sleep sleep for the specific number of milliseconds.
_sleep is a MSVC CRT function, and Sleep is a Windows API.
So in MSVC they should be interchangeable.

One minor difference is that in case of a 0 argument, _sleep sleeps for 1ms whereas Sleep doesn't sleep at all.

Share:
13,818
mlel
Author by

mlel

Updated on June 06, 2022

Comments

  • mlel
    mlel almost 2 years

    Currently, I am working on an old project developed (in C and C++) for Windows with Visual Studio 2010 or less. We would like to update it for newer version such as Visual Studio 2015 or 2017.

    I have found that the _sleep() function is no longer supported by Microsoft and that instead I shall use the Sleep() function.

    I didn't find the equivalent documentation for the old _sleep() function and I wonder if both functions behave exactly identical ? This MSDN post makes me wondering if the only differences are in the types of the argument ?

    Thanks in advance for your answers.

  • RbMm
    RbMm about 6 years
    simply look for slbeep.c - here _sleep implementation. not need any tests
  • David A. Gray
    David A. Gray about 4 years
    I'd count that as an improvement. Why would you ever want to sleep for zero milliseconds.
  • David Schwartz
    David Schwartz about 4 years
    @DavidA.Gray When you specify the amount of time to sleep, it's a minimum. The platform can put you to sleep for longer if that is beneficial to system performance. So a sleep for zero (or more) milliseconds says to the platform that now would be a good time to put you to sleep if it will help performance but if it won't help performance, you can continue to do useful work. It's commonly done at optimal pre-emption points such as right after releasing some locks (to give other threads a chance at it) or right before acquiring some (to reduce the odds you'll be pre-empted while holding it).
  • MSalters
    MSalters about 4 years
    This was especially useful before there were multicore processors. Sleep(0) is an effective way to hand back your control of the CPU core to the OS.
  • Human-Compiler
    Human-Compiler over 2 years
    This answer does not even address the question that was asked. The question is asking for the difference between the older/deprecated _sleep and Sleep, not how to use Sleep in a project. Also Sleep is a Win32 API function that can be included with #include <synchapi.h> -- you don't need the heavier <windows.h> header, or the UWP SDK.
  • Lundin
    Lundin over 2 years
    Also Sleep has been around for ages, it's not exactly a new function.
  • Sheri
    Sheri over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
  • Rick Lippens
    Rick Lippens over 2 years
    Oops, you are right. I read this backwards. The _sleep() function: I don't think I got working as it may be gone/unsupported, but as for Sleep() definitely this is still working. I had problems getting <synchapi.h> and <windows.h> working with Sleep() initially, but after I added the SDK it seemed to work. Very new to the newer Visual Studio apps, apologies for the newb answer.