getaddrinfo()
is part of the POSIX standard and the POSIX standard requires:
The freeaddrinfo() and getaddrinfo() functions shall be thread-safe.
Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html
An OS may not claim to be POSIX conforming if that wasn't the case.
Operating systems officially POSIX conforming that you may have heard of:
AIX, BSD, IRIX, macOS, (Open)Solaris, QNX, as well as several others.
On these platforms you can rely upon getaddrinfo()
being thread-safe.
Well known operating systems not officially POSIX conforming but always trying to stay as close to the POSIX standard as possible for software compatiblity:
BeOS, FreeBSD, GNU, iOS, Linux, NetBSD, OpenBSD, as well as several others.
On these platforms you cannot rely upon getaddrinfo()
being entirely thread-safe but you can certainly expect it to be thread-safe enough so that you can use it from multiple threads in your application without having to wrap any locking around it.
Note that getaddrinfo()
is also thread-safe on Linux as it will only become thread-unsafe if your code ever changes the locale or the environment while multiple threads are running and doing that is considered thread-unsafe on its own. So you can only make getaddrinfo()
thread-unsafe if you do something that is forbidden anyway (well, it's not really forbidden but you do it at your own risk as it is not safe to do so).
Also note that even if the man page would not have said that (some POSIX man pages say nothing about thread-safety), the POSIX standard actually mandates:
3.407 Thread-Safe
A thread-safe function can be safely invoked concurrently with other
calls to the same function, or with calls to any other thread-safe
functions, by multiple threads. Each function defined in the System
Interfaces volume of POSIX.1-2017 is thread-safe unless explicitly
stated otherwise. Examples are any "pure" function, a function which
holds a mutex locked while it is accessing static storage, or objects
shared among threads.
Source: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html