Thread sanitizer 给出了 "function race" 的假阴性

Thread sanitizer gives false negative for "function race"

考虑以下代码:

#include <atomic>
#include <iostream>
#include <thread>

std::atomic<int> x, y;
std::atomic<int> r1, r2;

void f()
{
  r1 = y.load();
  x = r1.load();
}

void g()
{
  r2 = x.load();
  y = 42;
}

int main()
{
  x = 0;
  y = 0;
  r1 = 0;
  r2 = 0;

  std::thread t1(f);
  std::thread t2(g);

  t1.join();
  t2.join();

  std::cout << r1 << " " << r2 << std::endl;
}
  1. 如果我使用 compilers/linux-x86_64-2.10.1/gnu7.1.0/bin/g++ -fsanitize=thread -O3 -std=c++11 main.cpp -o a.out 编译此代码,TSan 不会提供任何警告 and/or 线程错误。
  2. 但是,允许此代码生成 42 00 0 作为输出。
    • 如果g()f()开始之前执行,那么r1 = y.load()的值将是42
    • 如果g()f()开始之前没有被执行,那么r1 = y.load()的值为0
  3. 这是我应该 TSan 捕捉到的东西,还是我的期望在这里完全错误?
    • 如果我的预期是错误的,可以做什么(除了代码检查,这对于较大的代码库来说可能非常困难)来发现这样的错误?
    • 如果应该抛出一些错误,是否有一些我可能遗漏的特定选项(我使用文档 here 中指定的所有默认值)?

来自clang's documentation

ThreadSanitizer is a tool that detects data races

您没有数据竞争,因为您的所有变量都是原子的,所以没有什么可报告的。