Sleep() vs _sleep() functions
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.
Comments
-
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 theSleep()
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 about 6 yearssimply look for
slbeep.c
- here_sleep
implementation. not need any tests -
David A. Gray about 4 yearsI'd count that as an improvement. Why would you ever want to sleep for zero milliseconds.
-
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 about 4 yearsThis 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 over 2 yearsThis answer does not even address the question that was asked. The question is asking for the difference between the older/deprecated
_sleep
andSleep
, not how to useSleep
in a project. AlsoSleep
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 over 2 yearsAlso
Sleep
has been around for ages, it's not exactly a new function. -
Sheri over 2 yearsThis 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 over 2 yearsOops, 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.