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

我的设置:
-RedHad Linux 2.6
-g++ 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相似的虚假报告。

知道有什么问题吗?

最佳答案

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

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

09-11 19:28