From owner-freebsd-questions Thu Jul 20 14:28:40 2000 Delivered-To: freebsd-questions@freebsd.org Received: from ns.tar.com (ns.tar.com [204.95.187.2]) by hub.freebsd.org (Postfix) with ESMTP id 1845F37C13D for ; Thu, 20 Jul 2000 14:28:34 -0700 (PDT) (envelope-from dick@tar.com) Received: from test.tar.com (test [204.95.187.4]) by ns.tar.com (8.9.3/8.9.3) with ESMTP id QAA64541; Thu, 20 Jul 2000 16:28:32 -0500 (CDT) (envelope-from dick@tar.com) Received: by test.tar.com (Postfix, from userid 1000) id 7AC0081D4E; Thu, 20 Jul 2000 16:28:31 -0500 (CDT) Date: Thu, 20 Jul 2000 16:28:31 -0500 From: "Richard Seaman, Jr." To: Tan Juay Kwang Cc: freebsd-questions@FreeBSD.ORG Subject: Re: pthreads on 4.0-STABLE Message-ID: <20000720162831.B404@tar.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0.1i In-Reply-To: ; from tanjk@i-dns.net on Thu, Jul 20, 2000 at 03:30:31PM +0800 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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