【问题标题】:cannot get Helgrind/DRD work with C++11 thread无法让 Helgrind/DRD 与 C++11 线程一起工作
【发布时间】:2014-06-05 12:46:15
【问题描述】:

我在让 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 的类似虚假报告。

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 具体怎么做?
    • 把函数放在一个单独的cpp文件中,编译成目标文件并链接到你的可执行文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2014-05-03
    • 1970-01-01
    • 2020-02-29
    • 2011-04-25
    • 2014-11-22
    • 2014-05-15
    相关资源
    最近更新 更多