Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 3 Dec 1999 15:59:09 +0800
From:      Chih-hung Feng <chfeng@eagle.seed.net.tw>
To:        freebsd-questions@freebsd.org
Subject:   Is gethostbyname() multithread-safe?
Message-ID:  <19991203155909.A11513@eagle.seed.net.tw>

next in thread | raw e-mail | index | archive | help
Hi:

I am writing a multithreaded proxy-like program on FreeBSD 3.x
platform using POSIX thread library, and comming across a problem.
When the program had been running for a while, it sometimes stopped
responding anything. After further investigation, it turned out
that all threads stopped at the same line of code:

    pserver = (struct hostenv *)gethostbyname(servername);

Thus I am suspecting it was caused by something like resource
locking or race condition inside gethostbyname() call. Tried to
work around this I modified the code as:

    pthread_mutex_lock(&mutex);
    pserver = (struct hostent *)gethostbyname(servername);
    pthread_mutex_unlock(&mutex);

I'm not sure this is the right way to do it. If gethostbyname()
is MT-safe then it does no better except adding extra performance
punishment. I'm not even sure the bug has been fixed (I made the
modification today and the time is too short to prove the problem
never comes back again).

I will appreciate any kind of feedback. Please cc your reponse 
to my mailbox cause I'm not on this mailing list. 

Best Regards

Chih-hung Feng
Seednet, Taiwan
chfeng@seed.net.tw


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?19991203155909.A11513>