便携式比较和交换(原子操作)C/C++ 图书馆?

发布于 2024-07-27 15:00:40 字数 322 浏览 4 评论 0原文

是否有任何小型库,可以将各种处理器的类似 CAS 的操作包装成可跨多个编译器移植的宏或函数?

PS. atomic.hpp Library 位于 boost::interprocess::detail 命名空间内。 作者拒绝将其打造成一个维护良好的公共图书馆。

让我们重新回答这个问题,看看是否还有其他选择?

Is there any small library, that wrapps various processors' CAS-like operations into macros or functions, that are portable across multiple compilers?

PS. The atomic.hpp library is inside boost::interprocess::detail namespace. The author refuses to make it a public, well maintained library.

Lets reopen the question, and see if there are any other options?

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

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

发布评论

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

评论(9

依 靠 2024-08-03 15:00:40

OPA(开放便携式原子)可能非常适合您的需求。 https://trac.mcs.anl.gov/projects/openpa/

它提供在 MIT 风格的许可下,跨多个平台的通用原子操作的一致 C API。 图书馆很小,当然可以满足您的大小要求。 当前的平台列表是:

  • 适用于 x86、x86_64、ia64、PPC 440 和 MIPS 5K 处理器的 GCC 内联汇编。 相同的架构还支持多个具有 GCC 兼容前端的编译器,例如 icc、PGI 和 IBM 的 xlc。
  • GCC 原子内在函数,因此支持大多数 GCC-4.1+ 安装。
  • SUN Solaris 原子操作库。
  • Windows NT 内在函数(尽管目前您必须做一些额外的工作才能在 Windows 上进行构建)。
  • 两个伪平台,基于 pthread 互斥体的模拟,可移植到其他不支持的平台(同时牺牲一些性能),以及用于有条件编译为单线程代码的代码中的“不安全”实现。

我从未在 C++ 程序中使用过它,尽管它应该只需很少的更改或无需更改即可工作。 如果您遇到麻烦,我很乐意调整它(只需发送邮件 [email protected] )。

OPA (Open Portable Atomics) could be a good fit for your needs. https://trac.mcs.anl.gov/projects/openpa/

It provides a consistent C API to common atomic operations across multiple platforms under an MIT-style license. The library is small and certainly meets your size requirements. The current platform list is:

  • GCC inline assembly for x86, x86_64, ia64, PPC 440, and MIPS 5K processors. Several compilers with GCC-compatible-ish front-ends are also supported on the same architectures, such as icc, PGI, and IBM's xlc.
  • GCC atomic intrinsics, so most GCC-4.1+ installations are supported.
  • The SUN Solaris atomic operations library.
  • Windows NT intrinsics (although you currently have to do a little bit of extra work to build on Windows).
  • Two pseudo-platforms, pthread mutex based emulation for portability to otherwise unsupported platforms (while sacrificing some performance), and an "unsafe" implementation for use in code that is conditionally compiled to be single-threaded code.

I've never used it in a C++ program, although it ought to work with little or no changes. I'd be happy to tweak it if you run into trouble (just mail [email protected]).

书间行客 2024-08-03 15:00:40

boost 进程间库可能就是您想要的——Atomic.hpp 包含文件包含针对各种平台和编译器的比较和交换实现。

The boost interprocess library might be what you are after -- the Atomic.hpp include file contains compare-and-swap implementations for a variety of platforms and compilers.

一个人练习一个人 2024-08-03 15:00:40

英特尔线程构建模块有一个很好的便携式atomic模板,可以满足您的需求。 但它是否是一个小图书馆当然可以争论。

Intel Threading Building Blocks has a nice portable atomic<T> template which does what you want. But whether it is a small library or not can of course be debated..

吻风 2024-08-03 15:00:40

您可能对 Glib 的原子操作 函数、

g_atomic_int_compare_and_exchange()

实现 感兴趣各种架构的 CAS 语义。
该实现本身相对容易理解,并且可以独立使用而无需太多努力,您可以在 svn.gnome.org/viewvc/ 下的 glib/trunk/glib/gatomic.{c,h} 下找到它。 希望这可以帮助!

You might be interested in Glib's Atomic Operations functions,

g_atomic_int_compare_and_exchange()

implements the CAS semantics for various architectures.
The implementation itself is relatively easy to understand and can be used stand-alone without too much effort, you can find it at svn.gnome.org/viewvc/ under glib/trunk/glib/gatomic.{c,h}. Hope this helps!

你穿错了嫁妆 2024-08-03 15:00:40

在 Mac OS X 和 Windows 上,您应该使用内置的 CompareAndSwap 函数(分别为 InterlockedCompareExchange() 和 OSAtomicCompareAndSwapPtrBarrier())。 因此,无论这些平台上的编译器如何,都可以工作。

在其他 Unix 上,这有点棘手,如果您使用 GCC 4.1 或更高版本,您可以只使用其内置的 __sync_val_compare_and_swap(),并且许多(尽管不是所有)unix 编译器都支持合理的 gcc 扩展,因为许多源自 Linux 的代码假设它们存在。

因此,如果您想以一种适用于 OS X 和 Windows 上所有处理器的大多数编译器以及其他平台上的 GCC 和其他一些编译器的方式包装它们,您应该这样做:

boolean CompareAndSwapPointer(volatile * void * ptr,
                                  void * new_value,
                                  void * old_value) {
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
  return OSAtomicCompareAndSwapPtr (old_value, new_value, ptr);
#elif defined(_MSC_VER)
  return InterlockedCompareExchange(ptr, new_value, old_value);
#elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
  return __sync_val_compare_and_swap(ptr, old_value, new_value);
#else
#  error No implementation
#endif
}

这尚未经过测试,但我认为它应该是正确的。 请注意所有操作系统库如何以不同的顺序获取参数;-)

显然,如果需要,您可以为不同的大小进行一些版本的比较和交换,并将它们包装在模板中。 API 大多基于 C,并将类型信息编码到函数中,这种方式对于习惯通过模板参数化类型的人来说有点令人讨厌。

On Mac OS X and Windows there are builtin CompareAndSwap functions you should be using anyway (InterlockedCompareExchange() and OSAtomicCompareAndSwapPtrBarrier() respectively). Thus will work regardless of the compilers on those platforms.

On other Unixes it is a bit trickier, if you are using GCC 4.1 or later you can just use its builtin __sync_val_compare_and_swap(), and many though not all unix compilers support reasonable gcc extensions since a lot of code originating on Linux assumes they are present.

So if you want to wrap them up in a way that works with most all compilers for all processors on OS X and Windows, and with GCC and some other compilers on other platforms you should do something like:

boolean CompareAndSwapPointer(volatile * void * ptr,
                                  void * new_value,
                                  void * old_value) {
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
  return OSAtomicCompareAndSwapPtr (old_value, new_value, ptr);
#elif defined(_MSC_VER)
  return InterlockedCompareExchange(ptr, new_value, old_value);
#elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
  return __sync_val_compare_and_swap(ptr, old_value, new_value);
#else
#  error No implementation
#endif
}

That is not tested, but I think it should be correct. Note how all the OS libraries take the args in different orders ;-)

Obviously you can do a few version for the different size compare and swaps and wrap them in templates if you want. The APIs are mostly C based and encode the type information into the functions in such a way that it is sort of nasty for people used to parameterizing types via templates.

〗斷ホ乔殘χμё〖 2024-08-03 15:00:40

Boehm 提供了 atomic_ops 项目的库。 不过不知道许可证。

There is the library of the atomic_ops project by Boehm. Dunno about the license, though.

王权女流氓 2024-08-03 15:00:40

有一个建议的 C++0x 兼容 Boost 原子库: http:// www.chaoticmind.net/~hcb/projects/boost.atomic/

这个库的目的是
提供原子的实现
升压操作,基于
C++0x 草案指定的接口
标准。 其目的是使
过渡到 std::atomic 很容易,如
以及提供一种编写代码的方法
使用可编译的 C++0x 功能
较旧的系统。

它显然还不是 Boost 的一部分,但您可以在此处查看评论线程: http://lists.boost.org/Archives/boost/2009/12/160195.php

Boost.Atomic 现在的形式是我
考虑将其称为发布。 它有
支持“真正的”原子变量
上:

  • gcc/x86,32 位(在 Linux、FreeBSD 上测试)
  • gcc/x86,64 位(在 Linux 上测试)
  • gcc/powerpc32(在 Linux、Mac OS X 上测试)
  • gcc/powerpc64(未经测试)
  • 通用 Win32(在 Win XP 上使用 Visual Studio Express 进行测试)

对于所有其他人来说,它会倒退
优雅地锁定操作。 那里
是正确的 Quickbook 文档,
包括希望具有说明性的
示例部分。

There is a proposed C++0x-compatible Boost atomics library: http://www.chaoticmind.net/~hcb/projects/boost.atomic/

The purpose of this library is to
provide an implementation of atomic
operations for boost, based on the
interface specified by the C++0x draft
standard. It aims to make
transitioning to std::atomic easy, as
well as providing a means to make code
using this C++0x feature compilable on
older systems.

It's obviously not part of Boost yet, but you can check out the review thread here: http://lists.boost.org/Archives/boost/2009/12/160195.php

Boost.Atomic is now in a form that I
consider calling it a release. It has
support for "true" atomic variables
on:

  • gcc/x86, 32-bit (tested on Linux, FreeBSD)
  • gcc/x86, 64-bit (tested on Linux)
  • gcc/powerpc32 (tested on Linux, Mac OS X)
  • gcc/powerpc64 (untested)
  • generic Win32 (tested with Visual Studio Express on Win XP)

For all others it falls back
gracefully to locked operation. There
is proper quickbook documentation,
including a hopefully illustrative
example section.

ぇ气 2024-08-03 15:00:40

作者所说的(在您提供的链接中)是“我认为您可以安全地使用它们,直到出现一些官方的 Boost 库”。 将接口更改推迟到“原子函数将出现在 C++0x 中时”。

无论您今天使用什么,您都可能希望在新的 std:: 功能可用时迁移到该功能。

boost 的东西总体来说相当不错,看起来它被用于实现已发布的 Boost 库。 我也曾多次想使用该实现。

我会去的。

What the author said (in the link you provided) was "I think you can use them safely until some official Boost library comes". Deferring the interface change until "when atomic functions are going to be present in C++0x".

Whatever you use today, you're likely going to want to migrate to new std:: functionality when it's available anyway.

The boost stuff is generally pretty good, looks like it's used in the implementation of a released Boost library. I've also been tempted to use that implementation a few times.

I'd go for it.

奶气 2024-08-03 15:00:40

您还可以从 http://www.ioremap.net/node/224 ,这是相当新的(也许太新了),但它正在 Elliptics Network 中使用,所以它确实得到了(一些?)测试。

它还为您提供了 CAS 旁边的更高级别原语:RCU(读取复制更新),用于线程之间的无锁同步。

但这取决于您所说的“便携式”的含义:它支持 x86 和 PPC 架构、Linux、FreeBSD、OpenBSD、Solaris 和 MacOSX 操作系统,但……不支持 Windows。

而且许可证是 GPL,你可以讨厌也可以喜欢。

You could also look at libsync for inspiration from http://www.ioremap.net/node/224 , which is quite new (maybe too new), but it is being used in the Elliptics Network so it does get (some?) testing.

It also gives you higher level primitives next to CAS: RCU (Read Copy Update) for lockless synchronisation between threads.

But it depends on what you mean by 'portable': it supports archtectures x86 and PPC, OSes Linux, FreeBSD, OpenBSD, Solaris and MacOSX but ... no Windows.

And the license is GPL, which you can hate or love.

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