便携式比较和交换(原子操作)C/C++ 图书馆?
是否有任何小型库,可以将各种处理器的类似 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
OPA(开放便携式原子)可能非常适合您的需求。 https://trac.mcs.anl.gov/projects/openpa/
它提供在 MIT 风格的许可下,跨多个平台的通用原子操作的一致 C API。 图书馆很小,当然可以满足您的大小要求。 当前的平台列表是:
我从未在 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:
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]).
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.
英特尔线程构建模块有一个很好的便携式
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..您可能对 Glib 的原子操作 函数、
实现 感兴趣各种架构的 CAS 语义。
该实现本身相对容易理解,并且可以独立使用而无需太多努力,您可以在 svn.gnome.org/viewvc/ 下的 glib/trunk/glib/gatomic.{c,h} 下找到它。 希望这可以帮助!
You might be interested in Glib's Atomic Operations functions,
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!
在 Mac OS X 和 Windows 上,您应该使用内置的 CompareAndSwap 函数(分别为 InterlockedCompareExchange() 和 OSAtomicCompareAndSwapPtrBarrier())。 因此,无论这些平台上的编译器如何,都可以工作。
在其他 Unix 上,这有点棘手,如果您使用 GCC 4.1 或更高版本,您可以只使用其内置的 __sync_val_compare_and_swap(),并且许多(尽管不是所有)unix 编译器都支持合理的 gcc 扩展,因为许多源自 Linux 的代码假设它们存在。
因此,如果您想以一种适用于 OS X 和 Windows 上所有处理器的大多数编译器以及其他平台上的 GCC 和其他一些编译器的方式包装它们,您应该这样做:
这尚未经过测试,但我认为它应该是正确的。 请注意所有操作系统库如何以不同的顺序获取参数;-)
显然,如果需要,您可以为不同的大小进行一些版本的比较和交换,并将它们包装在模板中。 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:
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.
Boehm 提供了 atomic_ops 项目的库。 不过不知道许可证。
There is the library of the atomic_ops project by Boehm. Dunno about the license, though.
有一个建议的 C++0x 兼容 Boost 原子库: http:// www.chaoticmind.net/~hcb/projects/boost.atomic/
它显然还不是 Boost 的一部分,但您可以在此处查看评论线程: http://lists.boost.org/Archives/boost/2009/12/160195.php
There is a proposed C++0x-compatible Boost atomics library: http://www.chaoticmind.net/~hcb/projects/boost.atomic/
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 库”。 将接口更改推迟到“原子函数将出现在 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.
您还可以从 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.