views:

60

answers:

2

Some POSIX functions are not threadsafe. Examples are dirname and pathname.

The dirname() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.

On some platforms there are reentrant versions of dirname and pathname: dirname_r and pathname_r. As far as I found out there are not reentrant version of dirname and pathname on Linux.

  • Is that correct?
  • What is the alternative?
A: 

I'm not aware of reentrant versions of dirname and basename in Linux.

However, there's various reentrant implementations of both functions in different non-standard libraries.

g_path_get_dirname and g_path_get_basename from glib are probably commonly used, and their implementation doesn't depend too much on the rest of the glib library either, so it'd be easy enough to just borrow from their implementation if you don't particularly feel like linking against glib.

rafl
A: 

Is that correct?

According to the man page, yes.

What is the alternative?

Make it thread safe yourself, i.e. wrap the calls in a function that guards basename/dirname and copies the result into a supplied buffer while holding a mutex

nos