On a Ubuntu 12.04 system when I try compiling the following code:

#include <atomic>
int a;
int main()
{
  a = 0;
  std::atomic_thread_fence(std::memory_order_acquire);
  a = 1;
}

I get an error message like:

g++ test.cpp -std=c++0x 
/tmp/ccayKntC.o: In function `main': test.cpp:(.text+0x14): undefined reference to `std::atomic_thread_fence(std::memory_order)' collect2: ld returned 1 exit status

This happens when compiling with clang++ also. Since it is a linker error I guess that my version of libstdc++ lacks the necessary functionality. However, other atomic operations seem to work.

I'm using Ubuntu 12.04. I'm wondering if there is a problem with my system setup, whether it is a missing feature from my libstdc++, or possibly something else. And ideally I'd like to be able to fix the problem.

有帮助吗?

解决方案

This is actually a bug which was fixed in the 4.7 branch:

I think you need to use __sync_synchronize or something like __asm__ __volatile__ ( "mfence" ::: "memory" )

Some people like to be very rigorous about which synchronization operation they need, but I think using mfence all the time will suffice for common cases.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top