gpt4 book ai didi

c++ - 异步过程调用中的 GetLastError 竞争

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:20 25 4
gpt4 key购买 nike

假设我正在使用 APC,其中过程和调用代码都使用 SetLastError 和 GetLastError。这会导致 GetLastError 产生不可预测的值。有什么办法可以解决这个问题吗?

VOID CALLBACK MyFunction(ULONG_PTR param)
{

SetLastError(1);
// Doing some stuff here which takes some time
// Expecting 1 but can I/should I get 0 here ?
printf("LastError: %d\n", GetLastError());
}

int APCtry()
{

SetLastError(0);

DWORD dummy = 0;

if (!QueueUserAPC(MyFunction, GetCurrentThread(), dummy))
{
return 0;
}

printf("LastError: %d\n", GetLastError());

SleepEx(100, TRUE);

//SetLastError(0); Edited and commented

printf("LastError: %d\n", GetLastError());

return 0;
}

最佳答案

在 MyFunction 中,您始终应该将 1 作为 LastError。

APCtry has already gone ahead after MyFunction started doing the time consuming part...

这是一个很大的困惑:你所有的代码都在单线程中执行!

SleepEx begin...
MyFunction...
SleepEx return
SetLastError(0); in APCtry()

所以 SleepEx 在 MyFunction 完成之前不会返回。所以你在 SleepEx 之后设置为最后一个错误(或设置 SleepEx 的内容)- 对 MyFunction 没有任何影响- 因为这一切只会在 MyFunction 之后发生导出。您在 MyFunction 中设置为最后一个错误的内容- 这是你必须到达这里的地方

编辑

为了更清楚地了解 SleepEx 内部发生了什么: enter image description here

关于c++ - 异步过程调用中的 GetLastError 竞争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39077198/

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