Reputation: 307
I'm using the -fsanitize=thread,undefined
option in my project. When running the thread, the sanitizer indicates a race.
// Compiled on g++ 11.4 with the command g++ ./main.cc -fsanitize=undefined,thread
#include <thread>
int main()
{
std::thread t1([]{});
std::thread t2([]{});
t1.join();
t2.join();
}
In this example, the thread sanitizer sees a race:
Write of size 8 at 0x7b0400000800 by thread T2:
#0 pipe <null> (libtsan.so.0+0x3fba8)
#1 __sanitizer::IsAccessibleMemoryRange(unsigned long, unsigned long) <null> (libubsan.so.1+0x1f3e3)
#2 std::thread::_State_impl<std::thread::_Invoker<std::tuple<main::{lambda()#2}> > >::~_State_impl() <null> (a.out+0x402111)
#3 execute_native_thread_routine <null> (libstdc++.so.6+0xdbadd)
Previous write of size 8 at 0x7b0400000800 by thread T1:
#0 pipe <null> (libtsan.so.0+0x3fba8)
#1 __sanitizer::IsAccessibleMemoryRange(unsigned long, unsigned long) <null> (libubsan.so.1+0x1f3e3)
#2 std::thread::_State_impl<std::thread::_Invoker<std::tuple<main::{lambda()#1}> > >::~_State_impl() <null> (a.out+0x4022e9)
#3 execute_native_thread_routine <null> (libstdc++.so.6+0xdbadd)
Here is a compilation on a different version of g++ and the latest version of clang. There, the sanitizer also detects a race.
Upvotes: 4
Views: 171