$ clang -v
Ubuntu clang version 15.0.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64
Test case
Data race detected
// bad case: data race detected
void testDataRace()
{
int32_t counter = 0;
std::thread t1([&counter] {
if (!counter)
counter = 1;
});
std::thread t2([&counter] {
if (!counter)
counter = 2;
});
t1.join();
t2.join();
}
Not detected when use atomic
// For user it is a bad case, but not detected
void testDataRaceAtomic()
{
std::atomic<int32_t> counter = 0;
std::thread t1([&counter] {
if (!counter)
counter = 1;
});
std::thread t2([&counter] {
if (!counter)
counter = 2;
});
t1.join();
t2.join();
}
As a user's perspective the behavior is unexpected.
It still happens in trunk, but it doesn't look like a bug to me.
From what I understand, TSAN only detects data races (as in, the kind of UB), not race conditions in general (as in, logic errors in concurrent code that are not UB per se). (1, 2)
Env
Test case
Data race detected
Not detected when use atomic
As a user's perspective the behavior is unexpected.