谁见过这个问题(多线程中atof 1/10000概率出错 )
linux os
main函数里面加了个alarm(1),就是调用datetime来每秒钟刷新一下时间
应用完全是单线程的,但是加上alarm以后就成了2个线程了
应用里面测试atof (ulibc的库)如下:
double res;
char arr[10];
int no = 100000;
memset(arr, 0, 10);
arr[0] = '1 ';
while(no --)
{
res = atof(arr);
if(dRes - 1.0 > 0.00001 || 1.0 - dRes > 0.00001)
{
printf( " arr = %s, atof res = %lf \n ", arr, res);
printf( " cycle = %d \n ", 100000 - no);
break;
}
}
有时候会出现如下结果:
arr = 1, atof res = 0.000000
cycle = 3238
arr = 1, atof res = 17742.000000
cycle = 78323
去掉alarm_handle测试上百万次都不会出现
怎么回事???
[解决办法]
有一些os,在线程切换时,默认是不保护浮点寄存器的。应该可以在创线程函数里加参数来可启保护
检查一下你的linux手册,看是不是这个问题。
[解决办法]
。。想想确实我说的不对,函数保存的寄存器不包括调用标准中规定的用来传参数和返回值的。这些寄存器确实需要线程切换或是中断时来保存。
我经常犯错误的,要是啥都懂我就在家里数钱了