Windows 8(1)比需要多睡1毫秒
Windows 8(.1) Sleep 1 more ms than needed
Sleep
在Windows 8.1
x64上总是比需要的时间多1毫秒。例如,Sleep(1)
持续大约2毫秒,Sleep(2)
- 3等。timeBeginPeriod
设置为1。在Windows 7
上按预期工作(没有多余的毫秒)。这种行为是正常的/可以修复吗?
#include <Windows.h>
#include <stdio.h>
#pragma comment(lib, "winmm.lib")
LARGE_INTEGER Frequency;
long long int GetCurrent()
{
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
return (1000000 * counter.QuadPart / Frequency.QuadPart);
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
timeBeginPeriod(1);
QueryPerformanceFrequency(&Frequency);
const unsigned int count = 1000;
long long int buffer[count];
long long int lastTime = GetCurrent(), currentTime;
for (unsigned int i = 0; i < count; i++)
{
currentTime = GetCurrent();
buffer[i] = currentTime - lastTime;
lastTime = currentTime;
Sleep(1);
}
timeEndPeriod(1);
FILE *file = fopen("log.txt", "w");
for (unsigned int i = 0; i < count; i++)
fprintf(file, "%ldn", buffer[i]);
fclose(file);
return EXIT_SUCCESS;
}
NtDelayExecution
解决方案Mehrdad。
static NTSTATUS (__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval) = (NTSTATUS (__stdcall*)(BOOL, PLARGE_INTEGER)) GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtDelayExecution");
LARGE_INTEGER delay;
unsigned int milliseconds = 1;
delay.QuadPart = (milliseconds > 1) ? -10000LL * (milliseconds - 1) : -1LL;
NtDelayExecution(false, &delay);
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux for Windows上编译C++代码时出错
- 在Windows上用C++裁剪HBITMAP
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 为什么我不能在带有 Chrono 的 Windows 上让循环在C++中旋转不到一毫秒?
- 如何在跨平台C++中以毫秒为单位获得系统启动/启动时间(它应该在Windows / IOS / Android / MA
- 为什么 Windows udp 接收套接字上的超时总是比 SO_RCVTIMEO 设置的长 500 毫秒
- Windows C++ UWP:等待 XX 毫秒,直到事件发生
- 我可以在windows上的c++上检索微秒或非常精确的毫秒吗
- 如何在不使用windows.h或Boost的情况下在Visual C++9.0中获得毫秒分辨率
- 使用Windows和c++的chrono为每个人安排一个1毫秒的事件——我是不是要求太多了?
- 如何在Windows上每毫秒做一件事或做得更好
- 为Windows c++测量时间,毫秒或微秒
- Windows 8(1)比需要多睡1毫秒
- 如何获得比Windows XP上的计时器滴答(15毫秒)更高精度的时间