Is inet_ntoa() threadsafe? - Unix

This is a discussion on Is inet_ntoa() threadsafe? - Unix ; It returns a pointer to (I assume) an internal static buffer but there doesn't appear to be a thread safe version. Does it have internal locking that makes it thread safe or should I use inet_ntop() ? B2003...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Is inet_ntoa() threadsafe?

  1. Is inet_ntoa() threadsafe?

    It returns a pointer to (I assume) an internal static buffer but there
    doesn't appear to be a thread safe version. Does it have internal
    locking that makes it thread safe or should I use inet_ntop() ?

    B2003

  2. Re: Is inet_ntoa() threadsafe?

    Boltar writes:
    > It returns a pointer to (I assume) an internal static buffer but there
    > doesn't appear to be a thread safe version. Does it have internal
    > locking that makes it thread safe or should I use inet_ntop() ?


    It is not required to be thread-safe.


  3. Re: Is inet_ntoa() threadsafe?

    Boltar wrote:
    > It returns a pointer to (I assume) an internal static buffer but there
    > doesn't appear to be a thread safe version. Does it have internal
    > locking that makes it thread safe or should I use inet_ntop() ?


    It returns a pointer to a static buffer...no internal locking could make
    it safe against other threads because you still need to access the
    buffer after the call returns.

    It could conceivably be made safe with a per-thread buffer, but there is
    no requirement for this.

    Chris

  4. Re: Is inet_ntoa() threadsafe?

    Chris Friesen wrote:
    > Boltar wrote:
    >> It returns a pointer to (I assume) an internal static buffer but there
    >> doesn't appear to be a thread safe version. Does it have internal
    >> locking that makes it thread safe or should I use inet_ntop() ?

    >
    > It returns a pointer to a static buffer...no internal locking could make
    > it safe against other threads because you still need to access the
    > buffer after the call returns.


    True, but you could make it thread-safe by using a per-thread buffer,
    similar to errno.

    Alex

  5. Re: Is inet_ntoa() threadsafe?

    Alex Fraser wrote:
    > Chris Friesen wrote:


    >> It returns a pointer to a static buffer...no internal locking could
    >> make it safe against other threads because you still need to access
    >> the buffer after the call returns.


    > True, but you could make it thread-safe by using a per-thread buffer,
    > similar to errno.


    I stated that in my post.

    Chris

  6. Re: Is inet_ntoa() threadsafe?

    Alex Fraser writes:
    >Chris Friesen wrote:
    >> Boltar wrote:
    >>> It returns a pointer to (I assume) an internal static buffer but there
    >>> doesn't appear to be a thread safe version. Does it have internal
    >>> locking that makes it thread safe or should I use inet_ntop() ?

    >>
    >> It returns a pointer to a static buffer...no internal locking could make
    >> it safe against other threads because you still need to access the
    >> buffer after the call returns.

    >
    >True, but you could make it thread-safe by using a per-thread buffer,
    >similar to errno.
    >
    >Alex


    Of course the prefered way to make it safe would be for the caller
    to provide the buffer address and length.

    scott

  7. Re: Is inet_ntoa() threadsafe?

    Boltar wrote:
    > It returns a pointer to (I assume) an internal static buffer but there
    > doesn't appear to be a thread safe version. Does it have internal
    > locking that makes it thread safe or should I use inet_ntop() ?
    >
    > B2003


    Depends on the OS. It is on Solaris, the man page has the following note:

    The return value from inet_ntoa() points to a buffer which
    is overwritten on each call. This buffer is implemented as
    thread-specific data in multithreaded applications.

    --
    Ian Collins.

  8. Re: Is inet_ntoa() threadsafe?

    Chris Friesen wrote:
    > Alex Fraser wrote:
    >> Chris Friesen wrote:

    >
    >>> It returns a pointer to a static buffer...no internal locking could
    >>> make it safe against other threads because you still need to access
    >>> the buffer after the call returns.

    >
    >> True, but you could make it thread-safe by using a per-thread buffer,
    >> similar to errno.

    >
    > I stated that in my post.


    My bad, I have no idea what I was doing .

    Alex

+ Reply to Thread