为什么OSX文件的atoi / ATOF作为是线程安全的? [英] Why does OSX document atoi/atof as not being threadsafe?

查看:332
本文介绍了为什么OSX文件的atoi / ATOF作为是线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,与strtol,当它涉及到非基本10 strtof是pferred给atoi / ATOF,因为前者发现错误,也与strtol比的atoi更灵活$ P $。

I understand that strtol and strtof are preferred to atoi/atof, since the former detect errors, and also strtol is much more flexible than atoi when it comes to non-base-10.

但我的东西仍然好奇:(!虽然不是在Linux上)人的atoi'(或ATOF)在OS X上提到的atoi / ATOF不是线程安全的。坦白说,我也很难想象一个可能实现的atoi或ATOF这不会是线程安全的。有谁知道为什么手册页说,这?这些功能在OS X或任何其他平台实际上是不安全的?如果是这样,为什么地球上不会图书馆只是与strtol来定义的atoi,因此是安全的?

But I'm still curious about something: 'man atoi' (or atof) on OS X (though not on Linux!) mentions that atoi/atof are not threadsafe. I frankly have a hard time imagining a possible implementation of atoi or atof that would not be threadsafe. Does anybody know why the man page says this? Are these functions actually unsafe on OS X or any other platform? And if they are, why on earth wouldn't the library just define atoi in terms of strtol, and therefore be safe?

推荐答案

在MacOS X上10.6.6手册页采取一看,它记录了两个函数, ATOF() atof_l(),我怀疑,给出了一个提示,为什么功能被认为不是线程安全的:

Taking a look at the manual page on MacOS X 10.6.6, it documents two functions, atof() and atof_l(), and I suspect that gives a hint as to why the function is deemed not thread-safe:

概要

#include <stdlib.h>
double atof(const char *str);

#include <xlocale.h>
double atof_l(const char *str, locale_t loc);


  
  

说明

ATOF()函数的字符串转换的初始部分指向STR翻一番重新presentation。

The atof() function converts the initial portion of the string pointed to by str to double representation.

这是等同于:

      strtod(str, (char **)NULL);


  
  

小数点字符在程序的语言环境(种类为LC_NUMERIC)中定义的。

The decimal point character is defined in the program's locale (category LC_NUMERIC).

虽然 ATOF()函数使用当前的语言环境,在 atof_l()函数可以通过一个语言环境直。有关更多信息,请参见xlocale(3)。

While the atof() function uses the current locale, the atof_l() function may be passed a locale directly. See xlocale(3) for more information.

实现注意

ATOF()函数不是线程安全的,也不是异步取消安全的。

The atof() function is not thread-safe and also not async-cancel-safe.

ATOF()功能通过 pcated被撤销$ P $的strtod(),不应使用在新的code。

The atof() function has been deprecated by strtod() and should not be used in new code.

错误

功能 ATOF()不必影响到一个错误的错误号的值。

The function atof() need not affect the value of errno on an error.

我怀疑的是,如果当前的区域被另一个线程,而 ATOF()函数执行改变,其结果不能保证。否则,似乎没有理由警告。

My suspicion is that if the current locale is changed by another thread while the atof() function is executing, the result is not guaranteed. Otherwise, there seems to be no reason for the warning.

我周围戳在达尔文C库源$ C ​​$ C的一个明确的位置,但还没有找到一个。如果你去了FreeBSD的code的<一个href=\"http://svn.freebsd.org/viewvc/base/stable/8/lib/libc/stdlib/atoi.c?revision=196045&view=markup\"相对=nofollow> 的atoi() ,很明显,功能实现很简单:

I've poked around for a definitive location of the Darwin C library source code, but have not found one. If you go to the FreeBSD source code for atoi(), it is clear that the function implementation is trivial:

int
atoi(str)
    const char *str;
{
    return (int)strtol(str, (char **)NULL, 10);
}

(是的,即使不使用原型定义!)

(Yes, not even using a prototyped definition!)

该名男子页与strtol()没有关于线程安全或黄鼠狼措辞异步取消安全。但是,快速浏览一下源$ C ​​$ C的<一个href=\"http://svn.freebsd.org/viewvc/base/stable/8/lib/libc/stdlib/strtol.c?revision=196045&view=markup\"相对=nofollow> 与strtol() 表明,使用 isspace为(),其中受影响按区域设置:

The man page for strtol() does not have the weasel wording about thread safety or async-cancel safety. However, a quick look at the source code for strtol() shows that it uses isspace(), which is affected by locale:

ISO / IEC 9899:1999,第7.11.1.1 setlocale函数

ISO/IEC 9899:1999, Section 7.11.1.1 The setlocale function

187 7.4的唯一功能,其行为不受当前语言环境是ISDIGIT和isxdigit判断。

187 The only functions in 7.4 whose behavior is not affected by the current locale are isdigit and isxdigit.

(凡§7.4是&LT;文件ctype.h方式&gt;

现在,虽然我不知道这code是相同的什么在达尔文(MacOS X系统),它很可能是相似的。我认为,有可能是在手册页空间勘误表 - 它不是那么清楚,需要更正的页面是否是一个的atoi()或一个与strtol()

Now, while I'm not sure that this code is identical to what's in Darwin (MacOS X), it is likely to be similar. I think that there could be room for errata in the man pages - it is not so clear whether the page that needs correction is the one for atoi() or the one for strtol().

这篇关于为什么OSX文件的atoi / ATOF作为是线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆