Search code examples
cglibc

Is hsearch_r thread-safe?


Can I use hcreate_r, hsearch_r and hdestroy_r in a thread-safe manner?

Do I have to wrap all calls to it with a mutex lock?


Solution

  • Quoting the manpage of HSEARCH(3):

    The hcreate_r(), hsearch_r(), and hdestroy_r() functions are thread-safe.

    So no, you do not need to wrap the calls with any sort of locking.

    In general, functions suffixed with _r tend to be re-entrant versions of the same function without the _r suffix (such as strtok_r). Their re-entrant nature (usually) makes them intrinsically thread-safe.