Is errno thread-safe ?
来源:互联网 发布:中国汽车 合并 知乎 编辑:程序博客网 时间:2024/05/03 14:28
Question:
In errno.h
, this variable is declared as extern int errno;
so my question is, is it safe to checkerrno
value after some calls or use perror() in multi-threaded code. Is this a thread safe variable? If not, then whats the alternative ?
I am using linux with gcc on x86 architecture.
Answer:
Yes, it is thread safe. On Linux, the global errno variable is thread-specific. POSIX requires that errno be threadsafe.
See http://www.unix.org/whitepapers/reentrant.html
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.
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):
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.
Also see 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.
- Is errno thread-safe ?
- what is thread safe
- Cocos2d-x is not thread-safe
- Thread-safe, Signal-safe
- thread safe
- Thread-Safe Singleton is Well Supported in C++11
- PHP Thread Safe and Non Thread Safe
- Thread Safe In Servlet
- Python: Thread safe Oject
- Older Thread-Safe Collections
- Thread-Safe Interface
- Thread safe circular queue
- Spring thread safe
- Thread-Safe Random Numbers
- Java Thread-safe
- libcurl thread safe
- Thread-Safe Class Design
- thread safe vs reentrant
- JS判断iframe是否加载成功
- 转载一篇-UML类图符号简介
- Git详解六 Git工具
- 4552
- SQL SERVER 2005/2008 中关于架构的理解(一)
- Is errno thread-safe ?
- hadoop的各方面日志定位
- Linux 目录结构
- LDA(线性判别分析或称Fisher线性判别),PCA(主成份分析)代码及表情识别中的应用
- 强悍——90后MM聊夜店
- 关于Thoughtworks在拉勾的那个面试题
- 传智博客(JavaWeb方面的所有知识)听课记录(经典)
- 安装ADT步骤
- php apc 缓存