Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 856
Jay Avatar asked May 18 '11 15:05

Jay


2 Answers

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.

like image 127
R.. GitHub STOP HELPING ICE Avatar answered Oct 16 '22 13:10

R.. GitHub STOP HELPING ICE


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

like image 1
AProgrammer Avatar answered Oct 16 '22 14:10

AProgrammer