c++ - 在多线程上下文中正确处理 GetLastError(和其他)

标签 c++ c multithreading winapi

假设 GetLastError(和变体)是每线程还是每进程是否正确?如果它是每个进程的问题在多线程应用程序中有些明显,因为无法保证在失败的调用和 GetLastError 之间没有进行其他 Win32 调用。有时 GetLastError 的值很重要。

例如,如果您正在使用 IO 完成端口,AcceptEx 将返回 FALSE(失败)。 WSAGetLastError(类似于 GetLastError)将返回 ERROR_IO_PENDING 以通知您它已挂起并且失败不是由于其他原因。问题是许多其他调用可能正在运行并覆盖此值。

这些调用是线程特定的还是进程特定的?如果特定于流程,那么您如何正确处理?

最佳答案

the docs对此绝对不含糊:

GetLastError Function

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.

所以他们说了三遍(在一个段落中!):应该足够了,因为 Lewis Carroll说;-)。因此,无需回答诸如“但是如果它是按进程而不是按线程,那么……呢?”之类的假设。;-)。

关于c++ - 在多线程上下文中正确处理 GetLastError(和其他),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3426035/

相关文章:

c++ - ACE 线程管理器无法调用函数

C程序: pass function as parameter to another function to create thread

c -/usr/bin/env : ‘python’ : Permission denied---Problem while installing Ninja

python - 如何加速与子进程的通信

c++ - 为什么输入后得到随机数?

c++ - 为什么使用指针 vector 被认为是不好的?

C++ 覆盖 std::string 的赋值 ( = ) 运算符

c++ - gcc '-rdynamic' 标志等同于 MSVC

Java:WAITING线程结果而不阻塞UI?

在c中创建一个pthread,使用它来等待创建线程完成执行