Static assertion for atomic read/write of value
Is there a way to check if the reading/writing (load/store) of a value is atomic? I have specialized versions of concurrent containers than can only work with such values and I would like to add a static assert to prevent accidental misuse.
For all the fundamental types 开发者_如何学JAVAon x86_64 this is true, but it may not be true for all platforms, or all long data types. Also, it is possible that small structures and unions will also be assigned with an atomic operation (since they'd just be compiled to use the same-size fundamental copy operation).
The C++0x draft has a section with macros in the <atomic> header, which indicates that there is no easy and portable way to check this otherwise.
29.4 Lock-free property [atomics.lockfree]
#define ATOMIC_CHAR_LOCK_FREE implementation-defined
#define ATOMIC_CHAR16_T_LOCK_FREE implementation-defined
#define ATOMIC_CHAR32_T_LOCK_FREE implementation-defined
#define ATOMIC_WCHAR_T_LOCK_FREE implementation-defined
#define ATOMIC_SHORT_LOCK_FREE implementation-defined
#define ATOMIC_INT_LOCK_FREE implementation-defined
#define ATOMIC_LONG_LOCK_FREE implementation-defined
#define ATOMIC_LLONG_LOCK_FREE implementation-defined
The macros indicates the types where std::atomic<type> can be implemented without a lock, which means that they are atomic in themselves.
The only method you have is to dump the generated assembly of each function and refer to the processor vendor's notes for the instructions atomicity guarantees.
精彩评论