Thread-safe DNS requests
Friday, November 2nd, 2007I’ve been steadily working on a chat client, similar to irssi for a few months now.
My design is mostly single-threaded, except for DNS queries.
I use a GThreadPool (part of the lovely glib library) to run DNS lookups.
I’d like to run several of these, which means I must use something that is thread-safe.
The gethostbyname() C function is a typical way of looking up the IP of a given hostname. However, this function is inherently not thread-safe. The gnu libc provides gethostbyname_r, which is re-entrant. I could use this, but then I lose portability to platforms that use other libc implementations, such as OS X or one of the *BSDs.
However, there is a standard (POSIX) thread-safe DNS lookup function! getaddrinfo()!
Now, the manpage for getaddrinfo(3) under OS X says it is not thread-safe, but this is actually just a result of a manpage not being updated since 2004. I wonder, are there any other (up-to-date) platforms where getaddrinfo is not thread safe?