cmultithreadingerrno

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?


Solution

  • 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:

    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.