现在的位置: 首页 > 综合 > 正文

由localtime引发的函数可重入问题

2014年09月14日 ⁄ 综合 ⁄ 共 1942字 ⁄ 字号 评论关闭

一、先看一个例子

#include<time.h>
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
	time_t now;
	struct tm *tmStart, *tmEnd;
	if(time(&now)==-1)//函数time返回月历值(格式 struct tm)。若参数是0,函数返回该值,否则将转换的结果保存在参数中。失败返回-1
		return -1;
	tmStart = localtime(&now);//把从1970-1-1零点零分到当前时间系统所偏移的秒数时间,转换为本地tm结构体的日历时间
	Sleep(7);//linux下Sleep改为sleep
	if(time(&now)==-1)
		return -1;
	tmEnd = localtime(&now);
	//按照预先的想法,tmStart比tmEnd的时间近似1897秒,可是结果并不是我们想要的!!!
	printf("tmStart时间为:\t%s", asctime(tmStart));
	printf("tmEnd时间为:\t%s", asctime(tmEnd));
	getchar();
	return 0;
}

结果是:

(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

void main()
{
	struct in_addr b1,b2;
	b1.s_addr = inet_addr("192.168.1.5");//将一个点分十进制的IP转换成一个长整数型数
	b2.s_addr = inet_addr("192.168.1.87");
	char*s1 = inet_ntoa(b1);//将一个in_addr格式的IP转换成一个互联网标准点分格式的字符串
	char*s2 = inet_ntoa(b2);
	//当上两句执行完毕之后,s1和s2指向相同的地址,这跟localtime类似
}

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.

抱歉!评论已关闭.