小编Os3*_*Os3的帖子

C++ 11(g ++线程已消毒)使用原子对非原子操作进行排序(误报?)

我正在尝试使用g ++和线程消毒剂,我认为我得到了误报.这是真的吗,还是我犯了一些大错?

程序(剪切和粘贴来自Anthony Williams:C++ Concurrency in Action,第145页,清单5.13)

#include <atomic>
#include <thread>
#include <assert.h>
bool x=false;
std::atomic<bool> y;
std::atomic<int> z;
void write_x_then_y()
{
  x=true;
  std::atomic_thread_fence(std::memory_order_release);
  y.store(true,std::memory_order_relaxed);
}
void read_y_then_x()
{
  while(!y.load(std::memory_order_relaxed));
  std::atomic_thread_fence(std::memory_order_acquire);
  if(x)
    ++z;
}
int main()
{
  x=false;
  y=false;
  z=0;
  std::thread a(write_x_then_y);
  std::thread b(read_y_then_x);
  a.join();
  b.join();
  assert(z.load()!=0);
}
Run Code Online (Sandbox Code Playgroud)

编译:

g++ -o a -g -Og -pthread a.cpp -fsanitize=thread
Run Code Online (Sandbox Code Playgroud)

g ++版本

~/build/px> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/6.1.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading c++11 thread-sanitizer

14
推荐指数
1
解决办法
510
查看次数

标签 统计

c++ ×1

c++11 ×1

multithreading ×1

thread-sanitizer ×1