我是否需要在编译时添加 _REENTRANT 宏以使我的 errno 线程安全?
我是否需要在编译时添加 _REENTRANT 宏以使我的 errno 线程安全?
如果不是,是所有版本的 gcc/linux/solaris 都是如此,还是某些旧版本需要?
我最近测试了一段未使用 _REENTRANT 的代码,发现 errno 在多线程环境中以未定义的方式表现?但是,添加 _REENTRANT 后一切正常。环境是Solaris。
但是,这里的讨论似乎并没有说必须添加_REENTRANT。我有点困惑。
另外,除了 _REENTRANT 之外,我是否应该添加任何其他选项或库以确保我的应用程序具有线程安全的 errno?
Is it required for me to add a _REENTRANT macro during compile time to make my errno thread safe?
If no, is it the case for all versions of gcc / linux / solaris or is it required for certain old versions?
I recently tested a piece of code where _REENTRANT was not used and found the errno behaving in an undefined fahsion in multi thread environment? But, after adding _REENTRANT everything was working fine. The Environment was Solaris.
But, the discussion here doesn't seem to say it is mandatory to add _REENTRANT. I am a little confused.
Also, apart from _REENTRANT should I add be adding any other options or libs to ensure my application has a thread safe errno?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,
_REENTRANT
是遗留垃圾,当时线程被认为是现有实现之上的扩展,并且标准库的默认行为不是线程安全的。现代实现不需要它,而且它从来都不是标准的。 (请注意,这也是一个用词不当,因为可重入和线程安全具有完全不同的含义。)理论上,POSIX 要求您通过 < code>getconf 如果您正在编译线程程序:
POSIX_V7_THREADS_CFLAGS
另一方面,gcc 有自己冲突的“可移植”方式来请求线程支持:
-pthread
选项,该选项通常添加线程工作所需的任何预定义宏和库。In practice,
_REENTRANT
is legacy junk from a time when threads were considered an extension hacked on top of existing implementations, and the default behavior of the standard library was not thread-safe. It should not be needed on modern implementations, and it was never standard. (Note that it's also a misnomer, since reentrant and thread-safe have radically different meanings.)In theory, POSIX requires you to query and use the following configuration options via
getconf
if you're compiling a threaded program:POSIX_V7_THREADS_CFLAGS
POSIX_V7_THREADS_LDFLAGS
On the other hand, gcc has its own conflicting "portable" way to request thread support: the
-pthread
option, which normally adds any predefined macros and libraries needed for threads to work.通常,您需要使用诸如
-mt
-pthread
-thread
之类的选项进行编译(对于 Sun CC 来说是这样,对于某些平台来说也是如此)海湾合作委员会)。通过此选项,您可以获得所需的定义。如果你不使用它,你可能会链接错误的库,甚至会出现代码生成问题(例如对静态变量初始化没有保护)。Usually you need to compile with an option like
-mt
-pthread
-thread
(that is true for Sun CC and, for some platforms, for gcc). With this option you get the define that you need. If you don't use it, you may link the wrong library or even get code generation problem (such as no protection for static variable intialisation).