Is it required for me to add a _REENTRANT macro during compile time to make my errno thread safe?

梦想的初衷 提交于 2019-12-05 04:21:24

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.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!