views:

359

answers:

3

Hi,

Is there free a portable (Windows, GNU/Linux & MacOSX) library providing a lock-free atomic swap function?

If not, how would it be implemented for each of these platforms? (x86 with VC++ or g++)

Thanks

+1  A: 

Depends what you want to swap. In assembler for x86 you might be able to get a "nearly" atomic xor swap, otherwise I'd go with some solution that uses locking, which will differ on Win32/{Linux,Darwin}.

If you are looking for a library, have a look at APR (Apache Portable Runtime) - http://apr.apache.org/

anselm
http://apr.apache.org/docs/apr/1.3/group__apr__atomic.html#ge45c529f14f8489102382bd3fd4cce22 in particular you should use the apr atomic compare and swap, using the same value as comparison. Put it in a while as it's not guaranteed that the swap will take place.
Lorenzo Boccaccia
+2  A: 

There's a lock-free library pending review in boost. Also if you dig into source of boost smart pointers library you will find atomic ops inlined for multiple platforms. Another one - Intel Threading Building Blocks has implementation of atomic<> template.

Nikolai N Fetissov
... and in C++0x as well.
janneb
Right ... which working compiler implementation are talking about?
Nikolai N Fetissov
+1  A: 

Boost has a set of macros for facilitating lock-free operations in a portable way.

ceretullis