Linux 上的互锁等效项

发布于 2024-07-07 11:06:18 字数 28 浏览 4 评论 0原文

在 C++ Linux 应用程序中,获取

In a C++ Linux app, what is the simplest way to get the functionality that the Interlocked functions on Win32 provide? Specifically, a lightweight way to atomically increment or add 32 or 64 bit integers?

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

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

发布评论

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

评论(7

鹊巢 2024-07-14 11:06:19

Apache Portable Runtime 中的 atomic 函数非常接近 Win32 InterlockedXXX 功能。

The atomic functions from the Apache Portable Runtime are really close to the Win32 InterlockedXXX functions.

末が日狂欢 2024-07-14 11:06:19

您可以在源代码中插入一些汇编代码,以直接使用 x68 互锁指令。

您应该使用lock xadd操作。

例如,请参阅

You can insert some assembly code in your source, to use x68 interlocked instructions directly.

You should use a lock xadd operation.

See for instance this.

柏林苍穹下 2024-07-14 11:06:19

GTK 和 QT 编程中使用的相当常见的 glib 库以及独立的库提供了各种原子操作。 请参阅 http://library.gnome.org/devel/glib /2.16/glib-Atomic-Operations.html 获取列表。 Interlocked 在 Win32 上支持的大多数操作都有 g_atomic 函数,并且在硬件直接支持这些操作的平台上,它们被内联为所需的汇编代码。

The fairly common glib library that's used in GTK and QT programming as well as standalone offers a variety of atomic operations. See http://library.gnome.org/devel/glib/2.16/glib-Atomic-Operations.html for a list. There are g_atomic functions for most of the operations that Interlocked supports on Win32, and on platforms where the hardware directly supports these, they are inlined as the needed assembly code.

赏烟花じ飞满天 2024-07-14 11:06:19

经过进一步审查,看起来很有希望。 是的,堆栈溢出。

Upon further review, this looks promising. Yay stack overflow.

定格我的天空 2024-07-14 11:06:18

只是用一些注释来澄清与 Linux 无关的问题。

RWM(读-修改-写)操作和那些不单步执行的操作需要硬件支持才能原子执行; 其中增量和减量,fetch_and_add等。

对于某些体系结构(包括I386,AMD_64和IA64),gcc内置了对原子内存访问的支持,因此不需要外部库。 在这里您可以阅读有关 API 的一些信息。

Just few notes to clarify the issue which has nothing to do with Linux.

RWM (read-modify-write) operations and those that do not execute in a single-step need the hardware-support to execute atomically; among them increments and decrements, fetch_and_add, etc.

For some architecture (including I386, AMD_64 and IA64) gcc has a built-in support for atomic memory access, therefore no external libray is required. Here you can read some information about the API.

笑忘罢 2024-07-14 11:06:18

Intel 的开源 ThreadBuildingBlocks 有一个模板 Atomic,它提供与 .NET 的 Interlocked 类相同的功能。

与 gcc 的 Atomic 内置函数不同,它是跨平台的,并且不依赖于特定的编译器。 正如 Nemanja Trifunovic 上面正确指出的那样,它确实取决于提供的 compare-and-swap CPU 指令由 x86 和 Itanium 芯片组成。 我想您不会对英特尔库有任何其他期望:)

Intel's open-source ThreadBuildingBlocks has a template, Atomic, that offers the same functionality as .NET's Interlocked class.

Unlike gcc's Atomic built-ins, it's cross platform and doesn't depend on a particular compiler. As Nemanja Trifunovic correctly points out above, it does depend on the compare-and-swap CPU instruction provided by x86 and Itanium chips. I guess you wouldn't expect anything else from an Intel library : )

你げ笑在眉眼 2024-07-14 11:06:18

严格来说,Linux 无法提供像 Win32 中那样的原子“互锁”功能,仅仅是因为这些功能需要硬件支持,而 Linux 运行在某些不提供该支持的平台上。 话虽如此,如果您可以将自己限制在 Intel x86/x64 上,请看一下 Boost 共享指针库中引用计数的实现。

Strictly speaking, Linux cannot offer atomic "interlocked" functions like ones in Win32, simply because these functions require hardware support, and Linux runs on some platforms that don't offer that support. Having said that, if you can constrain yourself to Intel x86/x64, take a look at the implementation of reference counting in Boost shared pointers library.

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