gpt4 book ai didi

c++ - 多线程实现中的errno

转载 作者:搜寻专家 更新时间:2023-10-31 00:16:02 26 4
gpt4 key购买 nike

要在多线程应用程序中使用 errno,请引用 http://www.cplusplus.com/reference/cerrno/errno/表示它应该在每个线程中本地实现。这是什么意思?

最佳答案

errno 应该是thread-local。在每个thread中这个变量的值可以不同。

that it should be locally implemented in every thread

errno 实现为thread_local 变量不是您的责任。它适用于编译器开发人员。

来自 cppreference.com

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/

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