无法让Helgrind / DRD与C ++ 11线程一起工作

时间:2014-06-05 12:46:16

标签: c++ multithreading c++11 race-condition concurrent-programming

让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。

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:2)

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

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