Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 10 Aug 1999 09:26:44 -0400
From:      Brian McGovern <bmcgover@cisco.com>
To:        ognir@humboldt1.com
Cc:        dan@trinsec.com, hackers@FreeBSD.ORG
Subject:   Re: gethostbyaddr() and threads. 
Message-ID:  <199908101326.JAA01963@bmcgover-pc.cisco.com>
In-Reply-To: Your message of "Mon, 09 Aug 1999 14:21:38 PDT." <199908092121.OAA21266@home.humboldt1.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
>> gethostbyaddr... actually, most of the gethostby* functions... are not
>> thread safe. They all use a static buffer in the library.
>> 
>> Therefore, with threads, if you don't take precautions, I'd expect your
>> results to be odd.
>>     -Brian
>> 
>Couldn't this be easily fixed? I haven't looked at the source yet, but I
>believe you could replaced the static buffers with a dynamically-allocated list
>of buffers, with one for each thread using the gethost functions. Or perhaps
>you could just eliminate all the static stuff altogether?

-Joe

You could, but thats not what was asked. Nor is the solution portable. Posix
warns that these functions are not thread safe. Realistically, the portable
solution would be to mutex lock (or equiv.) around the calls, so that only
one thread can call the function(s) at a time, and be sure to copy the results
out of the temporary buffers before releasing the locks. (see note on
"if you don't take precautions")

An even more interesting solution may be to write some wrappers that do the
lock, call the function, and store the result in a thread-specific area, then
unlock again.

	-Brian


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




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