错误号是线程安全的? [英] Is errno thread-safe?

查看:114
本文介绍了错误号是线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

errno.h中,这个变量声明为的extern INT错误号; 所以我的问题是,它是在多线程code几个电话或使用PERROR()后安全检查错误号值。这是一个线程安全的变量?如果没有,那么什么选择?

In errno.h, this variable is declared as extern int errno; so my question is, is it safe to check errno value after some calls or use perror() in multi-threaded code. Is this a thread safe variable? If not, then whats the alternative ?

我使用的Linux与x86架构的gcc。

I am using linux with gcc on x86 architecture.

推荐答案

是的,它是线程安全的。在Linux上,全局errno变量是线程特定的。 POSIX要求的errno是线程安全的。

Yes, it is thread safe. On Linux, the global errno variable is thread-specific. POSIX requires that errno be threadsafe.

请参阅http://www.unix.org/whitepapers/reentrant.html

在POSIX.1,errno将被定义为
  外部全局变量。但是这个
  定义是一个不可接受的
  多线程环境中,因为它的
  使用可导致非确定性
  结果。问题是,两个或
  更多的线程会遇到错误,所有的
  会引起相同errno设置。
  在这些情况下,线程
  可能最终会后,检查错误号
  已经由另一个更新
  主题。

In POSIX.1, errno is defined as an external global variable. But this definition is unacceptable in a multithreaded environment, because its use can result in nondeterministic results. The problem is that two or more threads can encounter errors, all causing the same errno to be set. Under these circumstances, a thread might end up checking errno after it has already been updated by another thread.

要规避由此而来
  非确定性,POSIX.1c重定义
  错误号为可以访问该服务
  每个线程错误如下号码
  (ISO / IEC 9945:1-1996,第2.4节):

To circumvent the resulting nondeterminism, POSIX.1c redefines errno as a service that can access the per-thread error number as follows (ISO/IEC 9945:1-1996, §2.4):

有些功能可以提供访问的变量错误号
  通过符号错误号。符号
  错误号是由包括定义
  头,由指定
  C标准......对于的每个线程
  过程中,errno的值不得
  通过函数调用或受到影响
  任务被其他线程给errno。

Some functions may provide the error number in a variable accessed through the symbol errno. The symbol errno is defined by including the header , as specified by the C Standard ... For each thread of a process, the value of errno shall not be affected by function calls or assignments to errno by other threads.

另请参见 http://linux.die.net/man/3/errno

错误号是本地线程;在一个线程设置它不会影响其在任何其他线程值。

errno is thread-local; setting it in one thread does not affect its value in any other thread.

这篇关于错误号是线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆