64位精度休眠功能?

64bit precision Sleep function?

目前 Sleep 时间采用单个 DWORD(32 位)。是否有采用 DWORDLONG(64 位)的替代方案?

我使用的是 RNG,每增加一个字节,总等待时间就会增加。使用 32 位整数,总时间是 5 分钟,我想增加它。

Is there any alternative which takes DWORDLONG (64bit)?

没有。您需要在一个循环中多次调用 Sleep

享受测试和调试多月睡眠的乐趣。

Sleep() 需要几毫秒。最大 DWORD 值 4294967295 将导致超时期限为 49.7 天。对于大多数用途,这是一个足够好的最大值,但如果您决定使用 64 位睡眠参数,则可以将多个 Sleep() 调用链接在一起。这会将您可以 Sleep() 的最大毫秒数更改为 18446744073709551615,这是数十万个世纪的数量级:

VOID WINAPI Sleep64(DWORDLONG dwlMilliseconds)
{
    while (dwlMilliseconds)
    {
        Sleep(min(0xFFFFFFFE, dwlMilliseconds));
        dwlMilliseconds -= min(0xFFFFFFFE, dwlMilliseconds);
    }
}

我已经测试过了,可以验证它是否有效。

Sleep[Ex]内部调用NtDelayExecution - undocumented but exist in all windows nt versions (from nt 4 to win 10) - exported by ntdll.dll - use ntdll.lib or ntdllp.lib from wdk. as result of this call in kernel will be called documented function KeDelayExecutionThread

//extern "C"
NTSYSAPI 
NTSTATUS
NTAPI
NtDelayExecution(
  IN BOOLEAN              Alertable,
  IN PLARGE_INTEGER       Interval );
  • Alertable

Specifies TRUE if the wait is alertable. Lower-level drivers should specify FALSE.

  • Interval

Specifies the absolute or relative time, in units of 100 nanoseconds, for which the wait is to occur. A negative value indicates relative time. Absolute expiration times track any changes in system time; relative expiration times are not affected by system time changes.

Sleep[Ex] is win32 shell, over this native api, 限制间隔值(从64位到32位)不能设置绝对时间(可以用NtDelayExecution) 并忽略警报(如果等待可警报,我们可以通过警报线程退出 NtDelayExecution)

所以你可以直接调用这个 api 而不是通过 Sleep[Ex]

间接调用

所以 Sleep(dwMilliseconds) 是调用 Sleep(dwMilliseconds, false)

SleepEx(dwMilliseconds, bAlertable) 

致电

LARGE_INTEGER  Interval;
Interval.QuadPart = -(dwMilliseconds * 10000);
NtDelayExecution(bALertable, &Interval);

请注意,如果可警告等待,它可以通过 apc (api return STATUS_USER_APC) 或通过警报 (STATUS_ALERTED 将是 return ed. 我们可以通过 NtAlertThread) 提醒线程。 SleepEx 检查 returned 状态,以防万一 STATUS_ALERTED - 再次开始等待更新的间隔。所以 SleepEx 等待不能通过警报 (NtAlertThread) 中断,但 NtDelayExecution 可以