Reputation: 2858
Consider the following code:
#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;
}
compilers/linux-x86_64-2.10.1/gnu7.1.0/bin/g++ -fsanitize=thread -O3 -std=c++11 main.cpp -o a.out
, TSan
does not provide any warnings and/or threading errors. 42 0
and 0 0
as output.
g()
is executed before f()
starts, then r1 = y.load()
will have a value of 42
g()
is not executed before f()
starts, then r1 = y.load()
will have a value of 0
. TSan
to catch, or are my expectations completely wrong here?
Upvotes: 1
Views: 1650
Reputation: 180955
ThreadSanitizer is a tool that detects data races
You don't have a data race since all of your variables are atomic, so there is nothing to report.
Upvotes: 4