5

In the UNIX® System Threads Reference, under the heading of "Thread-safety" is a list of functions are "not guaranteed to be thread-safe on all UNIX systems." The function scandir() is absent from this list, while readdir() appears on the list.

However the glibc source for scandir() clearly appears to call readdir(), not the thread-safe readdir_r(). So was scandir() omitted from the list for some other reason, or is it thread-safe for some reason I am missing?

Mike Godin
  • 3,727
  • 3
  • 27
  • 29
  • 1
    A newer version of the "not required to be thread-safe" list is here: http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_01 but scandir still isn't listed. Would be interesting to compare BSD and SysV-derived sources if you have the time to track them down. A bug report should be made on glibc, probably. –  Nov 18 '13 at 18:44
  • 1
    It appears that glibc actually implements readdir() in a thread-safe manner, so probably no bug report needs to be made. – Mike Godin Nov 20 '13 at 04:46

2 Answers2

1

I think, this list covers POSIX functions only. scandir(3) is BSD/SVID and might not be listed there hence. The new, thread-safe functions are probably the focus of this list but not listing old, thread-unsafe functions.

ensc
  • 6,704
  • 14
  • 22
1

It appears that POSIX.1-2008 specifies that scandir() is thread-safe, since it is a POSIX.1-2008 function, and not in the list of functions allowed to be non-threadsafe. However, POSIX.1-2008 does not preclude readdir() from being thread-safe, and in the case of glibc, it appears that the readdir() source actually is thread-safe, since it does not return a global struct dirent, rather it returns a glibc-defined member of the DIR type returned in the opendir() call.

So even though glibc's scandir() calls readdir(), it still appears to be thread-safe.

Mike Godin
  • 3,727
  • 3
  • 27
  • 29