views:

1612

answers:

1

I know that similar template exits in Intel's TBB, besides that I can't find any implementation on google or in Boost library.

+10  A: 

You can find discussions about this feature implementation in boost there : http://lists.boost.org/Archives/boost/2008/11/144803.php

> Can the N2427 - C++ Atomic Types and Operations be implemented

> without the help of the compiler?

No.

They don't need to be intrinsics if you can write inline assembler (or separately-compiled assembler for that matter) then you can write the operations themselves directly. You might even be able to use simple C++ (e.g. just plain assignment for load or store). The reason you need compiler support is preventing inappropriate optimizations: atomic operations can't be optimized out, and generally must not be reordered before or after any other operations. This means that even non-atomic stores performed before an atomic store have to be complete, and can't be cached in a register (for example). Also, loads that occur after an atomic operation cannot be hoisted before the atomic op. On some compilers, just using inline assembler is enough. On others, calling an external function is enough. MSVC provides _ReadWriteBarrier() to provide the compiler ordering. Other compilers need other flags.

Klaim