getaddrinfo()
is part of the POSIX standard, and the POSIX standard requires:
The functions freeaddrinfo () and getaddrinfo () must be thread-oriented.
Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html
The OS may not claim POSIX compliance if it is not.
Operating systems officially comply with POSIX, which you may have heard about:
AIX, BSD, IRIX, macOS, (Open) Solaris, QNX, as well as several others.
On these platforms, you can rely on getaddrinfo()
be thread-oriented.
Well-known operating systems that do not officially comply with POSIX, but always try to get as close as possible to the POSIX standard to ensure software compatibility:
BeOS, FreeBSD, GNU, iOS, Linux, NetBSD, OpenBSD, as well as several others.
On these platforms, you cannot rely on getaddrinfo()
completely thread safe, but you can of course expect it to be thread safe enough that you can use it from multiple threads in your application without having to wrap it around it.
Please note that getaddrinfo()
also thread-oriented on Linux, as it will become thread-safe only if your code ever changes the locale or environment while several threads work and do it, considering thread-safe unsafe in itself. Thus, you can make getaddrinfo()
thread-safe only if you do something that is forbidden in any case (well, actually it is not forbidden, but you do it at your own peril and risk, so how unsafe it is).
Also note that even if this is not said on the man page (some POSIX man pages say nothing about thread safety), the POSIX standard actually requires:
3.407 thread safe
A thread-safe function can safely be called simultaneously with other calls to the same function or with calls to any other thread-safe functions by multiple threads. Each function defined in the POSIX.1-2017 system interface volume is thread-oriented, unless otherwise specified. Examples are any βcleanβ function, a function that keeps the mutex locked while accessing static storage, or objects shared by threads.
Source: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html