博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux c readdir是非线程安全,需用readdir_r,要注意用静态变量当做返回值的函数的非线程安全性...
阅读量:7058 次
发布时间:2019-06-28

本文共 817 字,大约阅读时间需要 2 分钟。

readdir函数:      

struct dirent *readdir(DIR *dirp); 

The  data  returned by readdir() may be overwritten by subsequent calls to readdir() for the same directory stream.

成功时,readdir() 返回指向 dirent 结构的指针。(这个结构是静态分配的;不要试图去free(3) 它。)如果到达了上当结尾,NULL 被返回并保持ERRNO不变。如果错误发生了,NULL 被返回并小心设置 ERRNO值。

readdir函数为非线程安全函数;

解决方法:

1、加锁;

2、用局部变量保存数据。

readdir_r()就是采用局部变量保存数据;

int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);

The  readdir_r() function returns 0 on success.  On error, it returns apositive error number (listed under ERRORS).  If the end of the  directory  stream  is  reached,  readdir_r()  returns 0, and returns NULL in*result.

readdir_r() 函数是 readdir() 函数可重入版本。它从目录流dirp 里读取下一个目录项,并且通过调用者分配的缓存区 entry返回。返回条目的指针被放置于 *result 里;如果目录流到达结尾,那么把*result 设置为 NULL。

转载于:https://www.cnblogs.com/cloudwind2011/p/8315388.html

你可能感兴趣的文章
hadoop集群环境搭建-hadoop之伪分布搭建环境
查看>>
动态编译
查看>>
分享:一个基于NPOI的excel导入导出组件(强类型)
查看>>
数据结构实验之二叉树的建立与遍历
查看>>
C++基础之迭代器
查看>>
杂题 洛谷P2018 消息传递
查看>>
算法笔记 --- Radix Sort
查看>>
Jetty的配置
查看>>
scala函数等号省略
查看>>
通过AutoConfig实现Form Server配置文件的修改 【转载】
查看>>
20165324 2017-2018-2 《Java程序设计》课程总结
查看>>
8-unittest中case管理
查看>>
ExtJs XTemplate
查看>>
[转载[工具]]PLSQL使用技巧
查看>>
用户管理的设计--1.首页查询功能实现
查看>>
PHP保留两位小数
查看>>
numpy 数组相减
查看>>
getRequestDispatcher(path).forward(),,执行完,后面的代码居然还会执行!!!记得加return 啊亲...
查看>>
如何安装和配置RabbitMQ
查看>>
08-jQuery的位置信息
查看>>