标准库中有执行原子操作的函数吗?

发布于 2024-08-22 11:21:34 字数 167 浏览 9 评论 0原文

C 运行时库或任何其他实用程序库是否支持执行原子操作(例如整数的递增/递减)等函数?

如果是,那么使用此类函数可以使哪些操作成为原子操作?

使用此类函数会比互斥体等普通同步原语更有利吗?

操作系统:Windows、Linux、Solaris 和 Linux VxWorks

Are there functions for performing atomic operations (like increment / decrement of an integer) etc supported by C Run time library or any other utility libraries?

If yes, what all operations can be made atomic using such functions?

Will it be more beneficial to use such functions than the normal synchronization primitives like mutex etc?

OS : Windows, Linux, Solaris & VxWorks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾其所爱 2024-08-29 11:21:34

在 C11 之前,

C 库没有任何库。

在 Linux 上,gcc 提供了一些 - 查找 __sync_fetch_and_add__sync_fetch_and_sub 等。

对于 Windows,请查找 InterlockedIncrement、InterlockedDecrement、InterlockedExchange 等。如果您在 Windows 上使用 gcc,我猜它也具有与 Linux 上相同的内置函数(尽管我尚未验证这一点)。

在 Solaris 上,这取决于情况。想必如果您使用 gcc,它可能(再次)具有与 Linux 下相同的内置函数。除此之外,还有一些图书馆,但没有真正标准化的图书馆。

C11

C11 添加了一组(相当)完整的原子操作和原子类型。这些操作包括诸如 atomic_fetch_addatomic_fetch_sum 之类的内容(以及相同的 *_explicit 版本,可让您指定所需的排序模型,其中默认的排序模型始终使用memory_order_seq_cst)。还有fence函数,例如atomic_thread_fenceatomic_signal_fence

这些类型对应于每个普通整数类型 - 例如,atomic_int8_t 对应于 int8_tatomic_uint_least64_t 对应于 uint_least64_t代码>.这些是 中定义的 typedef 名称。为了避免与任何现有名称发生冲突,您可以省略标头;编译器本身使用实现者命名空间中的名称(例如,_Atomic_uint_least32_t而不是atomic_uint_least32_t)。

Prior to C11

The C library doesn't have any.

On Linux, gcc provides some -- look for __sync_fetch_and_add, __sync_fetch_and_sub, and so on.

In the case of Windows, look for InterlockedIncrement, InterlockedDecrement``,InterlockedExchange`, and so on. If you use gcc on Windows, I'd guess it also has the same built-ins as it does on Linux (though I haven't verified that).

On Solaris, it'll depend. Presumably if you use gcc, it'll probably (again) have the same built-ins it does under Linux. Otherwise, there are libraries floating around, but nothing really standardized.

C11

C11 added a (reasonably) complete set of atomic operations and atomic types. The operations include things like atomic_fetch_add and atomic_fetch_sum (and *_explicit versions of same that let you specify the ordering model you need, where the default ones always use memory_order_seq_cst). There are also fence functions, such as atomic_thread_fence and atomic_signal_fence.

The types correspond to each of the normal integer types--for example, atomic_int8_t corresponding to int8_t and atomic_uint_least64_t corrsponding to uint_least64_t. Those are typedef names defined in <stdatomic.h>. To avoid conflicts with any existing names, you can omit the header; the compiler itself uses names in the implementor's namespace (e.g., _Atomic_uint_least32_t instead of atomic_uint_least32_t).

路弥 2024-08-29 11:21:34

“有益”是视情况而定的。性能始终取决于环境。当您将互斥体切换为类似的东西时,您可能会期望发生一些奇妙的事情,但您可能不会得到任何好处(如果它不是那么受欢迎的情况)或者使事情变得更糟(如果您不小心创建了“自旋锁”) 。

'Beneficial' is situational. Always, performance depends on circumstances. You may expect something wonderful to happen when you switch out a mutex for something like this, but you may get no benefit (if it's not that popular of a case) or make things worse (if you accidently create a 'spin-lock').

夏雨凉 2024-08-29 11:21:34

在所有受支持的平台上,您可以使用 GLib 的原子操作 。在内置原子操作(例如汇编指令)的平台上,glib 将使用它们。在其他平台上,它将回退到使用互斥体。

我认为原子操作可以提高速度,即使互斥体是使用它们实现的。使用互斥体,您将至少有两个原子操作(锁定和解锁)以及实际操作。如果原子操作可用,则它是单个操作。

Across all supported platforms, you can use use GLib's atomic operations. On platforms which have atomic operations built-in (e.g. assembly instructions), glib will use them. On other platforms, it will fall back to using mutexes.

I think that atomic operations can give you a speed boost, even if mutexes are implemented using them. With the mutex, you will have at least two atomic ops (lock & unlock), plus the actual operation. If the atomic op is available, it's a single operation.

瑶笙 2024-08-29 11:21:34

不确定 C 运行时库是什么意思。适当的语言或标准库不为您提供任何方法来执行此操作。您需要使用特定于操作系统的库/API。另外,不要被 sig_atomic_t 愚弄——它们并不像乍一看那样,仅在信号处理程序的上下文中有用。

Not sure what you mean by the C runtime library. The language proper, or the standard library does not provide you with any means to do this. You'd need to use a OS specific library/API. Also, don't be fooled by sig_atomic_t -- they are not what it seems at first glance and are useful only in the context of signal handlers.

剑心龙吟 2024-08-29 11:21:34

在 Windows 上,有 InterlockedExchange 和喜欢。对于 Linux,您可以采用 glibc 的原子宏 - 它们是可移植的(请参阅i486 原子.h )。我不知道其他操作系统的解决方案。

一般来说,您可以在 x86 上使用 xchg 指令进行原子操作(也适用于双核 CPU)。

至于你的第二个问题,不,我不认为使用原子操作会比使用互斥体更快。例如,pthreads 库已经实现了带有原子操作的互斥锁,速度非常快。

On Windows, there are InterlockedExchange and the like. For Linux, you can take glibc's atomic macros - they're portable (see i486 atomic.h). I don't know a solution for the other operating systems.

In general, you can use the xchg instruction on x86 for atomic operations (works on Dual Core CPUs, too).

As to your second question, no, I don't think that using atomic operations will be faster than using mutexes. For instance, the pthreads library already implements mutexes with atomic operations, which is very fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文