如何使用具有警报真实性和整体最短睡眠时间的 SleepEx? [英] How to use SleepEx with alertable true AND an overall minimum sleep time?

查看:68
本文介绍了如何使用具有警报真实性和整体最短睡眠时间的 SleepEx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用例:我当前的线程需要做operation1,等待一些时间与其他人协调,然后需要做操作2.在等待之间,由于文件系统事件,APC 可能需要由该线程处理,这些事件将另一个 operation1 添加到某个队列中,以便在 operation2 完成后稍后处理当前线程.像下面这样的简单:

I have the following use case: My current thread needs to do operation1, waits some amount of time to coordinate with others and afterwards needs to do operation2. In between the waiting, APCs might need to be processed by that thread because of file system events, which add another operation1 to some queue to be processed later after operation2 is finished by the current thread. Something simple like the following:

while (true)
{
  processOperation1;
  SleepEx(..., true);
  processOperation2;
}

重要的是,在operation1operation2 之间,至少要经过SleepEx 指定的时间!这不需要在一块,线程可以立即用于处理 APC 并将另一个 operation1 排队,除非指定,否则它不应该继续 operation2时间已经过去了.

The important thing is that between operation1 and operation2 at least the specified amount of time to SleepEx has to be elapsed! That doesn't need to be in one piece, the thread can instantly be used to process APCs and queue another operation1, it just shouldn't continue with operation2 unless the specified amount of time has elapsed.

来自 文档:

如果参数为 TRUE 并且调用此函数的线程与调用扩展 I/O 函数(ReadFileEx 或 WriteFileEx)的线程相同,则该函数在超时期限已过或 I/O 完成回调函数发生.如果发生 I/O 完成回调,则调用 I/O 完成函数.如果 APC 排队等待线程 (QueueUserAPC),则该函数会在超时时间已过或 APC 函数被调用时返回.

If the parameter is TRUE and the thread that called this function is the same thread that called the extended I/O function (ReadFileEx or WriteFileEx), the function returns when either the time-out period has elapsed or when an I/O completion callback function occurs. If an I/O completion callback occurs, the I/O completion function is called. If an APC is queued to the thread (QueueUserAPC), the function returns when either the timer-out period has elapsed or when the APC function is called.

根据我的理解,这意味着如果调用 SleepEx 并且 APC 已排队,则由当前线程直接执行,因为它有能力这样做.但是在 SleepEx 之后的代码之后会发生什么呢?线程是否因为 SleepEx 返回控制而返回处理 operation2 或者线程是否重新进入睡眠状态,保持在 SleepEx 直到指定的时间过期了吗?

From my understanding that means that if SleepEx is called and an APC has been queued, that is directly executed by the current thread because it is capable to do so. But what happens afterwards with the code after SleepEx? Does the thread return to process operation2 because SleepEx returned control or does the thread gets back to sleep, staying in SleepEx until the specified amount of time is elapsed?

文档中的第一句话不是说从函数返回,而是恢复线程":

The first sentence in the docs is not speaking about returning from the function, but "resuming the thread":

挂起当前线程,直到满足指定条件.发生以下情况之一时,将恢复执行:

Suspends the current thread until the specified condition is met. Execution resumes when one of the following occurs:

这可能意味着线程被恢复,处理 APC,然后停留在 SleepEx 中,无论需要多少时间都可以休眠.

That could mean that the thread is resumed, processes APCs and afterwards stays in SleepEx, sleeping for whatever amount of time is needed.

如果情况不是这样并且 SleepEx 真的离开了,它是否说明已经过去了多长时间?SleepEx 似乎没有提供该值,而只是提供了一些常量返回值.这听起来像我需要在 SleepEx 之前和之后自己花时间并在循环中一次又一次地调用该函数,直到我真正需要的时间已经过去?是否已经有这样的东西,也许作为 boost 的一部分?

If that's not the case and SleepEx is really left, does it tell how much time has elapsed? SleepEx doesn't seem to provide that value, but only some constant return value. This sounds like I would need to take time before and after SleepEx on my own and call that function again and again in a loop until the time I need really has elapsed? Is there already something out there like that, maybe as part of boost?

谢谢!

推荐答案

您应该在循环中调用 SleepEx 并处理排队的 APC,直到所需的超时时间过去.

You should call SleepEx in a loop and process the queued APCs until the needed timeout elapses.

类似的东西.

for (DWORD dwStart = GetTickCount(); ; )
{
    DWORD dwElapsed = GetTickCount() - dwStart;
    DWORD dw = (dwTimeout > dwElapsed) ? (dwTimeout - dwElapsed) : 0;
    if (!SleepEx(dw, TRUE))
        break;
}

这篇关于如何使用具有警报真实性和整体最短睡眠时间的 SleepEx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆