便携式比较和交换(原子操作)C/C++ 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1158374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Portable Compare And Swap (atomic operations) C/C++ library?
提问by
Is there any small library, that wrapps various processors' CAS-like operations into macros or functions, that are portable across multiple compilers?
是否有任何小型库可以将各种处理器的类 CAS 操作包装到宏或函数中,并且可以跨多个编译器移植?
PS.The atomic.hpp libraryis inside boost::interprocess::detail namespace. The author refuses to make it a public, well maintained library.
附注。该atomic.hpp库是升压::进程间::详细的命名空间中。作者拒绝使它成为一个公共的、维护良好的图书馆。
Lets reopen the question, and see if there are any other options?
让我们重新打开问题,看看是否还有其他选择?
采纳答案by Kristian
Intel Threading Building Blockshas 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..
Intel Threading Building Blocks有一个很好的可移植atomic<T>
模板,可以满足您的需求。但它是否是一个小型图书馆当然可以争论。
回答by Dave Goodell
OPA (Open Portable Atomics) could be a good fit for your needs. https://trac.mcs.anl.gov/projects/openpa/
OPA(开放便携式原子)可能非常适合您的需求。 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:
它在 MIT 风格的许可下为跨多个平台的常见原子操作提供了一致的 C API。该库很小,当然可以满足您的大小要求。当前平台列表为:
- 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.
- 用于 x86、x86_64、ia64、PPC 440 和 MIPS 5K 处理器的 GCC 内联汇编。几个具有 GCC 兼容型前端的编译器也受相同架构的支持,例如 icc、PGI 和 IBM 的 xlc。
- GCC 原子内在函数,因此支持大多数 GCC-4.1+ 安装。
- SUN Solaris 原子操作库。
- Windows NT 内在函数(尽管您目前必须做一些额外的工作才能在 Windows 上构建)。
- 两个伪平台,基于 pthread 互斥锁的仿真,可移植到其他不受支持的平台(同时牺牲一些性能),以及在有条件编译为单线程代码的代码中使用的“不安全”实现。
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]).
我从来没有在 C++ 程序中使用过它,尽管它应该很少或没有变化。如果您遇到麻烦,我很乐意对其进行调整(只需发送邮件至 [email protected])。
回答by Steve Gilham
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.
boost 进程间库可能正是您所追求的——Atomic.hpp 包含文件包含适用于各种平台和编译器的比较和交换实现。
回答by llongi
You might be interested in Glib's Atomic Operationsfunctions,
你可能对Glib 的原子操作函数感兴趣,
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!
为各种架构实现 CAS 语义。实现本身相对容易理解,可以单独使用,无需太多努力,您可以在 svn.gnome.org/viewvc/ 下的 glib/trunk/glib/gatomic.{c,h} 下找到它。希望这可以帮助!
回答by Louis Gerbarg
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.
在 Mac OS X 和 Windows 上,您应该使用内置的 CompareAndSwap 函数(分别为 InterlockedCompareExchange() 和 OSAtomicCompareAndSwapPtrBarrier())。因此,无论这些平台上的编译器如何,都可以工作。
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.
在其他 Unix 上,它有点棘手,如果您使用 GCC 4.1 或更高版本,则可以使用其内置的 __sync_val_compare_and_swap(),并且许多(但不是全部)unix 编译器都支持合理的 gcc 扩展,因为许多源自 Linux 的代码假定它们存在.
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:
因此,如果您想以一种适用于 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
}
That is not tested, but I think it should be correct. Note how all the OS libraries take the args in different orders ;-)
这没有经过测试,但我认为它应该是正确的。请注意所有操作系统库如何以不同的顺序获取 args ;-)
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.
显然,如果需要,您可以为不同大小的比较和交换做几个版本,并将它们包装在模板中。API 大多是基于 C 的,并将类型信息编码到函数中,对于习惯于通过模板参数化类型的人来说,这有点令人讨厌。
回答by Dirk
There is the library of the atomic_opsproject by Boehm. Dunno about the license, though.
有Boehm的atomic_ops项目库。不过,不知道许可证。
回答by Trevor Robinson
There is a proposed C++0x-compatible Boost atomics library: http://www.chaoticmind.net/~hcb/projects/boost.atomic/
有一个提议的 C++0x 兼容的 Boost 原子库: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.
该库的目的是基于 C++0x 草案标准指定的接口为 boost 提供原子操作的实现。它旨在使转换到 std::atomic 变得容易,并提供一种方法,使使用此 C++0x 功能的代码可在旧系统上编译。
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 的一部分,但您可以在此处查看评论主题: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.
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 测试)
对于所有其他人,它优雅地回退到锁定操作。有适当的快速手册文档,包括一个有希望的说明性示例部分。
回答by Marsh Ray
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".
作者说(在您提供的链接中)是“我认为您可以安全地使用它们,直到某些官方 Boost 库出现”。将接口更改推迟到“当原子函数将出现在 C++0x 中时”。
Whatever you use today, you're likely going to want to migrate to new std::
functionality when it's available anyway.
无论您今天使用什么,您都可能希望在新std::
功能可用时迁移到它。
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.
boost 的东西通常非常好,看起来它用于实现已发布的 Boost 库。我也曾多次尝试使用该实现。
I'd go for it.
我会去的。
回答by Rutger Nijlunsing
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.
您还可以从http://www.ioremap.net/node/224 中查看 libsync 的灵感,它很新(可能太新),但它正在 Elliptics Network 中使用,因此它确实得到了(一些?)测试。
It also gives you higher level primitives next to CAS: RCU (Read Copy Update) for lockless synchronisation between threads.
它还为您提供了 CAS 旁边的更高级别的原语:RCU(读取复制更新),用于线程之间的无锁同步。
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.
但这取决于您所说的“便携式”是什么意思:它支持架构 x86 和 PPC、操作系统 Linux、FreeBSD、OpenBSD、Solaris 和 MacOSX,但……不支持 Windows。
And the license is GPL, which you can hate or love.
许可证是 GPL,您可以讨厌或喜欢它。