gpt4 book ai didi

c++ - 对线程使用 GetLastError

转载 作者:行者123 更新时间:2023-11-30 01:46:18 27 4
gpt4 key购买 nike

不久前,我为 Windows API 函数编写了一个简单的包装器类。我编写了一组单元测试来验证该类生成的结果与直接调用 API 相匹配。

最近我回去添加了关于跨不同线程使用包装器类的单元测试。我发现 ::GetLastError 函数存在一些问题。根据 MSDN,该函数应保留每个线程的最后一个错误代码:

Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code.

我发现在某些情况下,最后一个错误代码实际上变成了零。我已经设法用下面的简单程序在单元测试之外复制了这个问题:

#include "stdafx.h"

#include <condition_variable>
#include <mutex>
#include <thread>
#include <Windows.h>

int main(int argc, char* argv[])
{
::DWORD setError1 = 123;
::DWORD setError2 = 456;

// scenario 1 - show that main thread not polluted by sub-thread
const auto act1 = [](::DWORD errorNo)
{
::SetLastError(errorNo);
const auto a = ::GetLastError(); // a = 123
};

::SetLastError(setError2);
const auto b = ::GetLastError(); // b = 456
const auto c = ::GetLastError(); // c = 456
std::thread sub1(act1, setError1);
sub1.join();
const auto d = ::GetLastError(); // d = 0 - WHY???


// scenario 2 - show that sub thread not polluted by main thread
std::condition_variable conditional;
std::mutex mutex;
bool flag = false;
::DWORD e;
const auto act2 = [&mutex, &flag, &e, &conditional](::DWORD errorNo)
{
std::unique_lock<std::mutex> lock(mutex);
::SetLastError(errorNo);
conditional.wait(lock, [&flag] { return flag; });
e = ::GetLastError(); // e = 456 in Windows 8.1, 0 in Windows 10.0.10240.0 - WHY???
};

std::thread sub2(act2, setError2);
{
std::lock_guard<std::mutex> guard(mutex);
::SetLastError(setError1);
flag = true;
}
conditional.notify_all();
sub2.join();
const auto f = ::GetLastError(); // f = 123;
return 0;
}

我遇到的问题是 de:

  • d - 我发现在使用子线程时,主线程 的最后一个错误代码会重置为零。

  • e - 使用 Windows 10 SDK 时,我发现 子线程 在等待 std::condition_variable< 时将最后一个错误重置为零。这在使用 Windows 8.1 SDK 时不会重置。

有人可以帮助解释我看到的结果吗?这是 Windows API 中的错误,还是微软 C++ 实现中的错误?还是我自己的代码中的错误?

最佳答案

这很简单。这是发生了什么:

  1. 您调用 SetLastError
  2. 您调用另一个函数,该函数又调用 SetLastError
  3. 您调用 GetLastError 并获取最新值,这是在第 2 项中设置的值,而不是在第 1 项中设置的值。

所以,看看你的代码,加上我的注释:

::SetLastError(setError2);          // item 1 from the list above
std::thread sub1(act1, setError1); // item 2
sub1.join(); // item 2
const auto d = ::GetLastError(); // item 3

还有你的另一个例子:

 ::SetLastError(errorNo);                          // item 1
conditional.wait(lock, [&flag] { return flag; }); // item 2
e = ::GetLastError(); // item 3

在每一种情况下,我已注释为第 2 项 的代码都在调用 API 函数,而 API 函数又会调用 SetLastError

关于c++ - 对线程使用 GetLastError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434555/

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