原文地址: http://blog.csdn.net/cuishumao/article/details/10162603
CSDN为什么没有转载功能呢???
一、先看一个例子
结果是:
(1)在windows、visual studio2010中的结果
(2)在linux、eclipse下的测试结果:
在linux下man localtime截取到的内容是:
注意到:
(1)The four functions asctime(), ctime(), gmtime() and localtime() return a pointer to static data and hence are not thread-safe.
这几个函数非线程安全的,线程安全的版本是在各函数后面加_r;
(2)The asctime(), ctime(), gmtime(), and localtime() functions shall return values in one of two static objects: a broken-down time structure
and an array of type char.即返回值保存在静态对象中。
二、不可重入函数
从以上例子的现象可以看出,tmStart指向的内容在后面的localtime中被改变了。也就是使用这种方法,前面的时间丢失了。
同样的问题若出在多线程中程序,自然出现不可预料的结果。
在实时系统中不可重入函数被认为是不安全的函数。
满足下列条件的函数多数是不可重入的:
(1) 函数体内使用了静态的数据结构;
(2)函数体内调用了malloc()或者free()函数;
(3)函数体内调用了标准I/O函数。
对于这种函数在处理时需要注意:当然最好使用线程安全的函数!
(1)对于这个例子,可以把tmStart的结果拷贝出来。使的该指针不在与localtime关联。
如:struct tm tmStart_pre;
memcpy(&tmStart_pre,tmStart,sizeof(struct tm));
(2)通过关中断、信号量(即P、V操作)等手段对其加以保护。
三、编写可重入的函数
(1)受保护的全局变量和静态变量
(2)尽量使用局部变量(例如寄存器、堆栈中的变量);
(3) 在和硬件发生交互的时候,切记执行类似disinterrupt()之类的操作,就是关闭硬件中断。完成交互记得打开中断,在有些系列上,这叫做“进入/退出核心”。
(4) 不能调用其它任何不可重入的函数。
(5) 谨慎使用堆栈。
四、与之类似的函数inet_ntoa
man inet_ntoa可以看到对它的解释:
The inet_ntoa() function converts the Internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation. The string is returned in a stati-cally allocated buffer, which subsequent calls will overwrite.