As mentioned in RbMm, it _sleepwas implemented as a very thin shell around Sleep:
void __cdecl _sleep(unsigned long dwDuration)
{
if (dwDuration == 0) {
dwDuration++;
}
Sleep(dwDuration);
}
To confirm, we can check it. Fortunately, it's easy to check:
#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
Thus, for a certain number of milliseconds it appears as _sleepwell as Sleepsleep.
_sleepis the MSVC CRT function, and Sleepis the Windows API.
Therefore, they must be interchangeable in MSVC.
One minor difference is that in the case of the argument 0 _sleepsleeps for 1ms, while they Sleepdo not sleep at all.
source
share