Both clang++
and g++
sanitizers produce similar warning about data race for this simple code. Is it a false alarm? What is the problem?
Code:
#include <thread>
struct A
{
void operator()()
{
}
};
struct B
{
void operator()()
{
}
};
int main(void)
{
// callable objects are created and moved into thread
std::thread t1(A{});
std::thread t2(B{});
t1.join();
t2.join();
return 0;
}
Compile flags:
-pthread -O0 -g -fsanitize=thread -fsanitize=undefined
Sanitizer output for g++
:
==================
WARNING: ThreadSanitizer: data race (pid=80173)
Write of size 8 at 0x7b0400000800 by thread T2:
#0 pipe ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1726 (libtsan.so.0+0x3ea28)
#1 __sanitizer::IsAccessibleMemoryRange(unsigned long, unsigned long) ../../../../src/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cpp:276 (libubsan.so.1+0x20102)
#2 std::thread::_State_impl<std::thread::_Invoker<std::tuple<B> > >::~_State_impl() /usr/include/c++/11/bits/std_thread.h:201 (a.out+0x5191)
#3 <null> <null> (libstdc++.so.6+0xdc2cb)
Previous write of size 8 at 0x7b0400000800 by thread T1:
#0 pipe ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1726 (libtsan.so.0+0x3ea28)
#1 __sanitizer::IsAccessibleMemoryRange(unsigned long, unsigned long) ../../../../src/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cpp:276 (libubsan.so.1+0x20102)
#2 std::thread::_State_impl<std::thread::_Invoker<std::tuple<A> > >::~_State_impl() /usr/include/c++/11/bits/std_thread.h:201 (a.out+0x53a5)
#3 <null> <null> (libstdc++.so.6+0xdc2cb)
Thread T2 (tid=80176, running) created by main thread at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:969 (libtsan.so.0+0x605b8)
#1 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) <null> (libstdc++.so.6+0xdc398)
#2 main a.cpp:20 (a.out+0x3396)
Thread T1 (tid=80175, finished) created by main thread at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:969 (libtsan.so.0+0x605b8)
#1 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) <null> (libstdc++.so.6+0xdc398)
#2 main a.cpp:19 (a.out+0x3383)
SUMMARY: ThreadSanitizer: data race ../../../../src/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cpp:276 in __sanitizer::IsAccessibleMemoryRange(unsigned long, unsigned long)
==================
ThreadSanitizer: reported 1 warnings
Note: This warning is given only when thread and UB sanitizers are both enabled.