要在多线程应用程序中使用 errno,请引用 http://www.cplusplus.com/reference/cerrno/errno/表示它应该在每个线程中本地实现。这是什么意思?
最佳答案
errno
应该是thread-local
。在每个thread
中这个变量的值可以不同。
that it should be locally implemented in every thread
将errno
实现为thread_local
变量不是您的责任。它适用于编译器开发人员。
errno is a preprocessor macro used for error indication. It expands to a thread-local modifiable lvalue of type int. (since C++11)
只是在 C++11 编译器中,这段代码永远不应该断言
#include <iostream>
#include <cerrno>
#include <thread>
#include <cassert>
int g_errno = 0;
void thread_function()
{
errno = E2BIG;
g_errno = errno;
}
int main()
{
errno = EINVAL;
std::thread thread(thread_function);
thread.join();
assert(errno != g_errno && "not multithreaded");
}
关于c++ - 多线程实现中的errno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17612112/