Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Jul 2000 16:28:31 -0500
From:      "Richard Seaman, Jr." <dick@tar.com>
To:        Tan Juay Kwang <tanjk@i-dns.net>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: pthreads on 4.0-STABLE
Message-ID:  <20000720162831.B404@tar.com>
In-Reply-To: <NEBBLKHLGDECFCNMHPEECEMPCOAA.tanjk@i-dns.net>; from tanjk@i-dns.net on Thu, Jul 20, 2000 at 03:30:31PM %2B0800
References:  <NEBBLKHLGDECFCNMHPEECEMPCOAA.tanjk@i-dns.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jul 20, 2000 at 03:30:31PM +0800, Tan Juay Kwang wrote:

> The first involves using pthread_create to generate several threads that
> basically calls gethostbyname(3). I realize that I can only create 2
> threads at the most. Anything more, gethostbyname will never return and
> thus the program will hang indefinitely. The same program runs fine on
> Solaris 7 and RedHat 6.2

AFAIK, FreeBSD's implementation of gethostbyname is not thread safe,
though I haven't studied it in detail, so its possible I'm wrong.
What happens if you wrap your calls to gethostbyname() with a mutex?
Does it work then?

In general, the specs I've seen (eg SUSv2) also stipulate that
even if gethostbyname is "thread safe" it can return a pointer
to a static data structure.  So, if you're going to use it in
multiple threads, and you want to be portable, you really need
to wrap gethostbyname() in a mutex even if its "thread safe" so
you use the returned pointer before releasing the mutex.  Otherwise
some other thread can stomp on the static structure before you
use it.

Ideally, a threads package will provide gethostbyname_r, where you
pass a buffer for the result so you can avoid having to wrap the
call in a mutex.  Unfortunately, I don't see this call in the
FreeBSD user threads package.  Such a call does exist in linuxthreads
(as well as the FreeBSD linuxthreads port --
 see /usr/ports/devel/linuxthreads).





-- 
Richard Seaman, Jr.        email:    dick@tar.com
5182 N. Maple Lane         phone:    262-367-5450
Nashotah WI 53058            fax:    262-367-5852


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000720162831.B404>