5

当我开始学习 valgrind(helgrind) 工具时,我遇到了一个我没能解决的棘手问题。

简单地说,用户定义的线程类是用一个虚函数创建的,该虚函数将由线程的入口例程调用。如果是这种情况,helgrind 将报告可能的数据竞争。但是在简单地省略 virtual 关键字之后,就不会报告此类错误。怎么会这样?我的代码有什么问题吗?或者有解决方法吗?

以下是演示此类问题的简单线程应用程序,包括 cpp、Makefile 和 helgrind 报告的消息。

/* main.cpp */
#include <memory.h>
#include <pthread.h>

class thread_s {
public:
  pthread_t       th;
  thread_s(void);
  ~thread_s(void);
  virtual void* routine(); /* if omit virtual, no error would be generated */
  void stop(void);
};
static void* routine(void*);
int main(int, const char*[])
{
  thread_s s_v;
  pthread_create(&s_v.th, 0, routine, &s_v);
  return 0;
}
static void* routine(void* arg)
{
  thread_s *pV = reinterpret_cast<thread_s*>(arg);
  pV->routine();
  return 0;
}
void* thread_s::routine(void)
{
  return 0;
}
thread_s::thread_s(void)
{
  th = 0;
}
thread_s::~thread_s(void)
{
  stop();
}
void thread_s::stop(void)
{
  void *v = 0;
  pthread_join(th, &v);
}

========================================

/* Makefile */
all: main test_helgrind

main: main.cpp
        g++ -o main main.cpp \
        -g -Wall -O0 \
        -lpthread

test_helgrind:
        valgrind \
                --tool=helgrind \
                ./main

clean:
        rm -f main

.PHONY: clean

========================================

g++ -o main main.cpp \
        -g -Wall -O0 \
        -lpthread
valgrind \
                --tool=helgrind \
                ./main
==7477== Helgrind, a thread error detector
==7477== Copyright (C) 2007-2010, and GNU GPL'd, by OpenWorks LLP et al.
==7477== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==7477== Command: ./main
==7477==
==7477== Thread #1 is the program's root thread
==7477==
==7477== Thread #2 was created
==7477==    at 0x4259728: clone (clone.S:111)
==7477==    by 0x40484B5: pthread_create@@GLIBC_2.1 (createthread.c:256)
==7477==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==7477==    by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)
==7477==    by 0x8048560: main (main.cpp:18)
==7477==
==7477== Possible data race during write of size 4 at 0xbeab24c8 by thread #1
==7477==    at 0x80485C9: thread_s::~thread_s() (main.cpp:35)
==7477==    by 0x8048571: main (main.cpp:17)
==7477==  This conflicts with a previous read of size 4 by thread #2
==7477==    at 0x804858B: routine(void*) (main.cpp:24)
==7477==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==7477==    by 0x4047E98: start_thread (pthread_create.c:304)
==7477==    by 0x425973D: clone (clone.S:130)
==7477==
==7477==
==7477== For counts of detected and suppressed errors, rerun with: -v
==7477== Use --history-level=approx or =none to gain increased speed, at
==7477== the cost of reduced accuracy of conflicting-access information
==7477== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 1 from 1)
4

2 回答 2

3

我不知道这是否是 helgrind 抱怨的原因,但您的程序存在严重问题。您创建一个线程,将指针传递给本地thread_s实例(s_vin main())。

但是,main()将很快返回,而不会与线程进行任何同步 -s_v当线程函数routine()获取指针并使用它调用pV->routine().

查看调用后添加以下内容是否pthread_create()可以防止 helmgrind 抱怨:

pthread_join( s_v.th, NULL);

实际上,更仔细地查看 helgrind 的输出,这几乎肯定会消除 helgrind 的抱怨,因为日志指向thread_s析构函数作为数据竞赛的参与者之一。

于 2012-08-31T09:34:57.713 回答
2

在一种情况下写入 vptr,在另一种情况下读取。两者都没有任何锁定。Helgrind 不知道您的程序中是否有其他方法使这种情况不可能同时在两个线程中发生,因此它会标记它。如果您可以保证在另一个线程中有人尝试对其调用函数时对象不会被破坏,那么您可以为此生成抑制。

于 2012-08-31T09:33:53.567 回答