Thread-safe or thread-compatible code is good. However there are cases in which one could implement things differently (more simply or more efficiently) if one knows that the program will not be using threads.
For example, I once heard that things like std::shared_ptr
could use different implementations to optimize the non-threaded case (but I can't find a reference).
I think historically std::string
in some implementation could use Copy-on-write in non-threaded code.
I am not in favor or against these techniques but I would like to know if that there is a way, (at least a nominal way) to determine at compile time if the code is being compiled with the intention of using threads.
The closest I could get is to realize that threaded code is usually (?) compiled with the -pthreads
(not -lpthreads
) compiler option.
(Not sure if it is a hard requirement or just recommended.)
In turn -pthreads
defines some macros, like _REENTRANT
or _THREAD_SAFE
, at least in gcc
and clang
.
In some some answers in SO, I also read that they are obsolete.
Are these macros the right way to determine if the program is intended to be used with threads? (e.g. threads launched from that same program). Are there other mechanism to detect this at compile time? How confident would the detection method be?
EDIT: since the question can be applied to many contexts apparently, let me give a concrete case:
I am writing a header only library that uses another 3rd party library inside. I would like to know if I should initialize that library to be thread-safe (or at least give a certain level of thread support). If I assume the maximum level of thread support but the user of the library will not be using threads then there will be cost paid for nothing. Since the 3rd library is an implementation detail I though I could make a decision about the level of thread safety requested based on a guess.
EDIT2 (2021): By chance I found this historical (but influential) library Blitz++ which in the documentation says (emphasis mine)
8.1 Blitz++ and thread safety
To enable thread-safety in Blitz++, you need to do one of these things:
- Compile with
gcc -pthread
, orCC -mt
under Solaris. (These options define_REENTRANT,which tells Blitz++ to generate thread-safe code).- Compile with
-DBZ_THREADSAFE
, or#define BZ_THREADSAFE
before including any Blitz++ headers.In threadsafe mode, Blitz++ array reference counts are safeguarded by a mutex. By default, pthread mutexes are used. If you would prefer a different mutex implementation, add the appropriate BZ_MUTEX macros to <blitz/blitz.h> and send them toblitz-dev@oonumerics.org for incorporation. Blitz++ does not do locking for every array element access; this would result in terrible performance. It is the job of the library user to ensure that appropriate synchronization is used.
So it seems that at some point _REENTRANT
was used as a clue for the need of multi-threading code.
Maybe it is a very old reference to take seriously.