std :: atomic_thread_fence有未定义的引用

时间:2013-05-07 22:31:31

标签: c++ c++11 ubuntu-12.04 libstdc++

在我尝试编译以下代码时,在Ubuntu 12.04系统上:

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

我收到如下错误消息:

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

在使用clang++进行编译时也会发生这种情况。由于它是链接器错误,我想我的libstdc ++版本缺少必要的功能。但是,其他原子操作似乎也有效。

我正在使用Ubuntu 12.04。我想知道我的系统设置是否有问题,是否是我的libstdc ++缺少的功能,或者可能是其他东西。理想情况下,我希望能够解决问题。

1 个答案:

答案 0 :(得分:2)

这实际上是一个在4.7分支中修复的错误:

我认为您需要使用__sync_synchronize__asm__ __volatile__ ( "mfence" ::: "memory" )

之类的内容

有些人喜欢对他们需要的which synchronization operation非常严格,但我认为一直使用mfence就足以应对常见情况。

相关问题