OS X 上 C 标准库的线程安全
Mac OS X 的 C 标准库实现中是否有明确的线程安全函数列表?
关于 glibc 和 f*()这里有一个很好的答案/code> 具有特定的功能,但我没有找到任何关于 OS X 的此类资源。有这样的东西吗?
例如,strptime() 和 strftime() 是线程安全的吗? printf()
?这些可能有一些我不想弄乱的内部缓冲区。 :)
Is there a definitive list of functions that are thread-safe in Mac OS X's implementation of the C standard library?
There is a good answer here with regards to glibc
and f*()
functions specifically, but I have failed to find any such resource with respect to OS X. Is there such a thing?
For example, are strptime()
and strftime()
thread-safe? printf()
? These are some that may have internal buffers that I would not want to mess up. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Single Unix 规范给出了一个相当简短的函数列表,允许非线程安全(除了“遗留功能组”中的函数允许是非线程安全的,尽管没有在那里列出)。该列表包括戴夫在回答中提到的
strtok()
,但不包括strptime()
,也不包括strftime()
,也不是printf()
。这个 StackOverflow 答案断言,在回答与此非常相似的问题时,OS X 确实支持上述方面规范,所以我认为这可能是最好使用的列表。 (顺便说一句,您可能还会对该问题的其余部分以及该问题的其他答案感兴趣。)
The Single Unix Specification gives a fairly short list of functions that are allowed to be non–thread-safe (except that functions in the "Legacy Feature Group" are allowed to be non–thread-safe despite not being listed there). The list includes
strtok()
, which Dave mentions in his answer, but does not includestrptime()
, norstrftime()
, norprintf()
.This StackOverflow answer asserts, in response to a question that is fairly similar to this one, that OS X does support the above aspect of the spec, so I think that's probably the best list to use. (You'll probably also be interested in the rest of that question, and in the other answer to it, by the way.)
任何看似具有神奇记忆能力的函数都可能不是线程安全的。任何返回您不希望使用 free() 的指针的函数通常都不是线程安全的。
您确实需要担心许多函数返回
char*
或struct foo*
。尽管这不是一个完美的规则,但这通常表明函数具有某种静态存储,并且不是线程安全的。strtok()
是 is 的一个简单示例,并且已被线程安全的strtok_r()
继承。对于许多非线程安全函数,存在一个 function_r() (r 表示可重入)。Any function which seems to have some magical remembering power, is likely not to be thread-safe. Any function which returns a pointer you aren't expected to
free()
is very frequently not thread-safe.Many of the functions you really have to worry about return
char*
, orstruct foo*
. Although this isn't a perfect rule, this is often indicative of a function which has some sort of static storage, and isn't thread-safe.strtok()
is a simple example of is, and has been succeeded bystrtok_r()
which is thread-safe. For many non-thread-safe functions, there exists afunction_r()
(r for reentrant) which is.