开发者

basename_r/dirname_r in Linux

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 p开发者_如何学Clatforms 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜