winapi - WaitForSingleObjectEx 是否允许提前唤醒?

标签 winapi synchronization

我正在总结一种情况,其中事件超时的 WaitForSingleObjectEx 没有返回“已发出信号”,但超时尚未过去。

具体来说,为了在 Qt 中调试这个问题,我更改了 this code :

bool QMutexPrivate::wait(int timeout)
{
    return (WaitForSingleObjectEx(event, timeout < 0 ? INFINITE : timeout, FALSE) == WAIT_OBJECT_0);
}

此代码(包括测量值):

bool QMutexPrivate::wait(int timeout)
{
    LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
    LARGE_INTEGER Frequency;

    QueryPerformanceFrequency(&Frequency);
    QueryPerformanceCounter(&StartingTime);

    auto result = (WaitForSingleObjectEx(event, timeout < 0 ? INFINITE : timeout, FALSE) == WAIT_OBJECT_0);

    QueryPerformanceCounter(&EndingTime);
    ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;

    ElapsedMicroseconds.QuadPart *= 1000000;
    ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
    if (!result && timeout > 0)
        std::cerr << "Waited for " << ElapsedMicroseconds.QuadPart << " microseconds when asked " << timeout << std::endl;
    return result;
}

其中事件是通过CreateEvent(0, FALSE, FALSE, 0);创建的。

强调此代码路径会打印如下语句:

Waited for 96734 microseconds when asked for 100 msecs

WaitForSingleObjectEx 是否允许提前唤醒,即在指定的超时之前(由于内核中的计时器合并或类似技术),或者我应该在其他地方调查我的问题? documentation并不能帮助我澄清这一点。

最佳答案

MSDN 上 Wait Functions and Time-out Intervals :

The accuracy of the specified time-out interval depends on the resolution of the system clock. The system clock "ticks" at a constant rate. If the time-out interval is less than the resolution of the system clock, the wait may time out in less than the specified length of time. If the time-out interval is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on.

To increase the accuracy of the time-out interval for the wait functions, call the timeGetDevCaps function to determine the supported minimum timer resolution and the timeBeginPeriod function to set the timer resolution to its minimum. Use caution when calling timeBeginPeriod, as frequent calls can significantly affect the system clock, system power usage, and the scheduler. If you call timeBeginPeriod, call it one time early in the application and be sure to call the timeEndPeriod function at the very end of the application.

关于winapi - WaitForSingleObjectEx 是否允许提前唤醒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38507918/

相关文章:

Java ConcurrentSkipListMap : adding atomically another Collection object

iphone - iPhone 和 iPad 之间的核心数据同步

c - 如何让EUDC在不重启的情况下生效?

python - 使用 win32gui 列出所有窗口

c++ - DirectSound:简单的 pcm 播放

multithreading - 类实例 "owning"TMultiReadExclusiveWriteSynchronizer 实例?

c - WriteFile 到标准输出失败

c++ - 在 Windows 10 上检测事件的虚拟桌面 - 任务 View - 虚拟桌面切换器

python - ElasticSearch 更新不是立即的,你如何等待 ElasticSearch 完成更新它的索引?

c# - volatile 作为一种同步机制