gpt4 book ai didi

c++ - 无法让 Helgrind/DRD 使用 C++11 线程

转载 作者:太空狗 更新时间:2023-10-29 21:42:59 35 4
gpt4 key购买 nike

我在让 Helgrind 和 DRD 使用 g++ 和 C++11 线程时遇到问题。

我的设置: - RedHad Linux 2.6 -克++ 4.7.2 - Valgrind 3.7.0

我试过贴出的程序 here ,在添加第一个答案中列出的定义之后,因此:

#include <valgrind/helgrind.h>
#define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(addr) ANNOTATE_HAPPENS_BEFORE(addr)
#define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(addr) ANNOTATE_HAPPENS_AFTER(addr)
#define _GLIBCXX_EXTERN_TEMPLATE -1

#include <thread>

int main()
{
std::thread t( []() { } );
t.join();
return 0;
}

然后我构建程序:

$ g++ -std=c++11 -Wall -Wextra -pthread main.cc

程序(没有做太多)正确运行:

$ ./a.out

还有 valgrind:

$ valgrind ./a.out
==21284== Memcheck, a memory error detector
==21284== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==21284== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==21284== Command: ./a.out
==21284==
==21284==
==21284== HEAP SUMMARY:
==21284== in use at exit: 0 bytes in 0 blocks
==21284== total heap usage: 2 allocs, 2 frees, 344 bytes allocated
==21284==
==21284== All heap blocks were freed -- no leaks are possible
==21284==
==21284== For counts of detected and suppressed errors, rerun with: -v
==21284== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

但是,对于 Helgrind,我得到了误报:

$ valgrind --tool=helgrind ./a.out                                                  
==21467== Helgrind, a thread error detector
==21467== Copyright (C) 2007-2011, and GNU GPL'd, by OpenWorks LLP et al.
==21467== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==21467== Command: ./a.out
==21467==
==21467== ---Thread-Announcement------------------------------------------
==21467==
==21467== Thread #1 is the program's root thread
==21467==
==21467== ---Thread-Announcement------------------------------------------
==21467== [lines removed]
==21467==
==21467== ----------------------------------------------------------------
==21467==
==21467== Possible data race during write of size 8 at 0x5B7A058 by thread #1
==21467== Locks held: none
==21467== [lines removed]
==21467==
==21467== This conflicts with a previous write of size 8 by thread #2
==21467== Locks held: none
==21467== at 0x4EE0A25: execute_native_thread_routine (shared_ptr_base.h:587)
==21467== by 0x4C2D3AD: mythread_wrapper (hg_intercepts.c:219)
==21467== by 0x55D1850: start_thread (in /lib64/libpthread-2.12.so)
==21467== by 0x58CF90C: clone (in /lib64/libc-2.12.so)
==21467==
==21467== [lines removed]
==21467==
==21467==
==21467== For counts of detected and suppressed errors, rerun with: -v
==21467== Use --history-level=approx or =none to gain increased speed, at
==21467== the cost of reduced accuracy of conflicting-access information
==21467== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

使用 DRD 而不是 Helgrind 的类似虚假报告。

知道哪里出了问题吗?

最佳答案

我找到了 this在drd手册中。看来您需要重新编译函数 execute_native_thread_routine() 和 std::thread::_M_start_thread(),并将其与您的程序链接(并且在这些函数的执行时不使用共享库)。

实际上,您的代码可以看到宏 _GLIBCXX_SYNCHRONIZATION_HAPPENS_*,但在构建此库时,它们不会被 c++ 库代码的内部函数看到。这就是为什么您需要使用您用于代码的相同 header 包含和宏定义来重新编译它们。

关于c++ - 无法让 Helgrind/DRD 使用 C++11 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24060626/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com