C标准库线程中的函数安全吗? [英] Are functions in the C standard library thread safe?

查看:215
本文介绍了C标准库线程中的函数安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以得到明确的答案,我的memcpy(使用Ubuntu随附的eglibc实现)是否是线程安全的? -老实说,我确实没有在文档中找到明确的是"或否".

Where can I get a definitive answer, whether my memcpy (using the eglibc implementation that comes with Ubuntu) is thread safe? - Honestly, I really did not find a clear YES or NO in the docs.

顺便说一句,线程安全"是指只要可以安全地同时复制一个字节的日期字节,就可以安全地并发使用memcpy.至少如果将只读数据复制到不重叠的区域,则应该可以做到这一点.

By the way, with "thread safe" I mean it is safe to use memcpy concurrently whenever it would be safe to copy the date byte for byte concurrently. This should be possible at least if read-only data are copied to regions that do not overlap.

理想情况下,我想在

Ideally I would like to see something like the lists at the bottom of this page in the ARM compiler docs.

推荐答案

您可以在2.9.1 Thread-Safety章中找到此列表:http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_01

You can find that list here, at chapter 2.9.1 Thread-Safety : http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_01

也就是说,这是posix 要求是线程安全的函数的列表.所有其他功能都必须是线程安全的. Posix包括标准C库和典型的"unix"接口. (完整列表在这里, http://pubs.opengroup.org/onlinepubs/9699919799/functions/contents.html )

That is, this is a list over functions that posix does not require to be thread safe. All other functions are required to be thread safe. Posix includes the standard C library and the typical "unix" interfaces. (Full list here, http://pubs.opengroup.org/onlinepubs/9699919799/functions/contents.html)

memcpy()由posix指定,但不属于2.9.1中的列表,因此可以认为是线程安全的.

memcpy() is specified by posix, but not part of the list in 2.9.1, and can therefore be considered thread safe.

Linux上的各种环境至少尝试尽其所能实现posix-即使posix不需要linux/glibc上的功能,它也可能是线程安全的-尽管很少记录.对于posix所不具备的其他功能/库,您只需保留其作者所记录的内容...

The various environments on linux at least tries to implement posix to the best of its abilities - The functions on linux/glibc might be thread-safe even if posix doesn't require it to be - though this is rarely documented. For other functions/libraries than what posix covers, you are left with what their authors have documented...

据我所知,posix将线程安全等同于可重入,并保证没有内部数据竞争.但是,您应对可能的外部数据竞赛负责-例如保护自己免于致电例如memcpy()具有可能同时更新的内存.

From what I can tell, posix equates thread safety with reentrancy, and guarantees there is no internal data races. You, however, are responsible for the possible external data races - such as protecting yourself from calling e.g. memcpy() with memory that might be updated concurrently.

这篇关于C标准库线程中的函数安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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