TSan 抱怨并行 for_each 循环中的数据竞争,但什么也不做

问题描述

这是一个小示例程序:

#include <execution>
#include <vector>

using namespace std;

int main() {
  vector<const char *> my_vector{"some","words","here"};
  for_each(execution::par,my_vector.begin(),my_vector.end(),[&](const char *s) {
             // do nothing here
           });
}

我编译它(在当前的 Arch Linux 上):

g++ -std=c++20 -ltbb -fsanitize=thread test.cc

我无法想象这个程序如何导致数据竞争,因为它没有写入,但 TSan 对此非常不满意。错误的洪流开始于:

WARNING: ThreadSanitizer: data race (pid=124291)                                                                                                                                                 Write of size 8 at 0x7f620b79b900 by thread T6:                                                                                                                                                  #0 memset /build/gcc/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:778 (libtsan.so.0+0x37449)                                                                    
    #1 memset /build/gcc/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:776 (libtsan.so.0+0x37449)                                                                    
    #2 <null> <null> (libtbbmalloc.so.2+0x13ed6)

这是否可能只是 TSan 中的误报?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)