Missing atomic_compare_exchange, why does code compile then fail to link?
I have been reading through and coding up examples from Anthony Williams'
book Concurrency in Practice and needed to enable
double-word-compare-and-exchange for gcc4.8 using -mcx16 so that a struct
containing a pointer an int could be manipulated in a lock-free atomic
manner.
I thought that atomic<> for user defined-types would be implemented with a
mutex by the compiler when double-word operations were not supported by
the platform. Even without double-word operations compare-exchange can be
made atomic by using a mutex, but not lock-free-atomic.
The following code gives linking errors in GCC4.8 and Clang 3.3 without
extra compiler options:
#include <atomic>
#include <thread>
struct ReferenceCountedPointer
{
int referenceCount;
void* data;
};
int main()
{
std::atomic<ReferenceCountedPointer> arcp;
ReferenceCountedPointer rcp;
arcp.compare_exchange_weak(rcp, rcp);
return 0;
}
The above program is pointless but illustrates the linking errors I see.
Compilation commands I used for Clang and GCC are:
Clang 3.3:
clang++-mp-3.3 -std=c++11 -stdlib=libc++ CX16.cpp -o CX16
Fails with:
Undefined symbols for architecture x86_64:
"___atomic_compare_exchange", referenced from:
_main in CX16-plVSvq.o
ld: symbol(s) not found for architecture x86_64
GCC4.8:
g++-mp-4.8 -std=c++11 CX16.cpp -o CX16
Fails with:
Undefined symbols for architecture x86_64:
"___atomic_compare_exchange_16", referenced from:
std::atomic<ReferenceCountedPointer>::compare_exchange_weak(ReferenceCountedPointer&,
ReferenceCountedPointer, std::memory_order, std::memory_order) in
ccOjp95s.o
ld: symbol(s) not found for architecture x86_64
Why do I get linking errors for atomic<> user defined (POD) types?
No comments:
Post a Comment