Thanks for reading this question, I only have basic C knowledge and I am not familiar with linux system. Here is what i know about readdir:
- someone said that readdir() is not multi thread safe beacuse it uses static varible,so that readdir can be described as this
struct dirent* readdir() {
static struct dirent; //read action
return &dirent;
}
- However, the actual glibc implementation is multi thread safe when you use readdir() to different dirstream (which is DIR struct), but not safe when you read the same dirstream (https://lwn.net/Articles/696474/)
- readdir_r has been deprecated since glibc 2.24. Here is the reason
https://man7.org/linux/man-pages/man3/readdir_r.3.html
My question:
- i did not see any static varible in glibc source code, where is the static varible?
Here is the source code I read. I can only see that it uses lock to ensure multi thread safe
https://github.com/lattera/glibc/blob/master/sysdeps/posix/readdir.c - I try to use multi thread to read same dirstream such as:
DIR* des_dir=opendir(xxx);
pthread_create(pid,0,custom_readdir,des_dir);
Each thread shares the same dirstream, and the result is OK. Each thread outputs part of the directory correctly, so how do I understand "In cases where multiple threads read from the same directory stream unsafe?" - If readdir() is indeed unsafe, how can I read a directory in a safe way? I did not find a function which can replace readdir().