小编Ozg*_*rat的帖子

这个简单的 C++ 代码中的数据竞争在哪里

clang++和消毒剂都会g++针对这个简单代码的数据竞争产生类似的警告。难道是虚惊一场?问题是什么?

代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

编译标志:

-pthread -O0 -g -fsanitize=thread -fsanitize=undefined
Run Code Online (Sandbox Code Playgroud)

消毒剂输出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 …
Run Code Online (Sandbox Code Playgroud)

c++ thread-sanitizer ubsan

16
推荐指数
1
解决办法
1527
查看次数

标签 统计

c++ ×1

thread-sanitizer ×1

ubsan ×1