gpt4 book ai didi

c++ - WaitForThreadpoolWaitCallbacks 立即返回,无需等待

转载 作者:行者123 更新时间:2023-12-02 02:34:36 25 4
gpt4 key购买 nike

我在这里做错了什么,WaitForThreadpoolWaitCallbacks 立即返回而不等待?

HANDLE hEvent = CreateEvent(0, FALSE, FALSE, 0);
...
PTP_WAIT Pwa = CreateThreadpoolWait(WaitCallback, NULL, NULL);
SetThreadpoolWait(Pwa, hEvent, NULL);
// Here we should wait until hEvent gets signaled
WaitForThreadpoolWaitCallbacks(Pwa, FALSE);
// Here we should get after hEvent gets signaled

WaitForThreadpoolWaitCallbacks 从不等待,并且在 WaitForThreadpoolWaitCallbacks 之后立即继续执行。但是,如果事件收到信号,回调仍然会以任何方式被调用。无论 WaitForThreadpoolWaitCallbacks 之前是否被调用,该事件都会收到信号。

在SetThreadPoolWait中将Timeout设置为NULL以永远等待。

A pointer to a FILETIME structure that specifies the absolute or relative time at which the wait operation should time out. If this parameter points to a positive value, it indicates the absolute time since January 1, 1601 (UTC), in 100-nanosecond intervals. If this parameter points to a negative value, it indicates the amount of time to wait relative to the current time. For more information about time values, see File Times.

If this parameter points to 0, the wait times out immediately. If this parameter is NULL, the wait will not time out.

编辑: 单独调用 SetThreadpoolWait 似乎不会将线程池等待项 (PTP_WAIT) 设置为挂起状态。如果是这种情况,如果 CancelPendingCallbacks 参数设置为 FALSE,WaitForThreadpoolWaitCallbacks 将等待。 “取消待定”一词也可能具有误导性。待处理也可能意味着一直在等待的事件已经发出信号,但没有空闲的工作线程来启动回调方法。

另一个不太可能的原因可能是 WaitForThreadpoolWaitCallbacks 有错误。

最佳答案

似乎可能存在竞争条件:

来自MSDN Magazine

The catch is that the wait object will only queue a callback when either the associated synchronization object is signaled or the timeout expires. Until one of those events occurs, no callbacks are queued and there’s nothing for the Wait function to wait for. The solution is to first call SetThreadpoolWait with null pointer values, telling the wait object to cease waiting, and then call WaitForThreadpoolWaitCallbacks to avoid any race conditions:

SetThreadpoolWait(w.get(), nullptr, nullptr);
WaitForThreadpoolWaitCallbacks(w.get(), TRUE);

关于c++ - WaitForThreadpoolWaitCallbacks 立即返回,无需等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16414729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com