当尝试创建只读取并打印自己的参数然后返回的单个线程时,helgrind会发现很多可能的数据争用,尽管主线程在创建新线程后立即执行pthread_join。 / p>
这是线程初始化(仍然会重现问题的缩小版本):
void liveness(cfg_t* cfg)
{
vertex_t* u;
size_t i;
size_t* arg;
pthread_t thread;
pthread_mutex_t* lock;
lock = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
if (lock == NULL) {
printf("Error when allocating memory for locks");
}
if (pthread_mutex_init(lock, NULL) != 0) {
printf("Error when creating lock\n");
}
arg = malloc(sizeof(size_t));
(*arg) = 0;
if (pthread_create(&thread, NULL, thread_start, arg)) {
perror("Error when creating thread\n");
exit(1);
}
if (pthread_join(thread, NULL)) {
perror("Error when joining thread\n");
exit(1);
}
free(lock);
free(arg); //244
}
这是thread_start
void* thread_start(void* arguments)
{
size_t index;
index = * (size_t*) arguments; /155
printf("Thread started! Index %zu\n", index);
fflush(stdout);
return NULL;
}
输出正确(线程已启动!索引0)但helgrind产生以下输出
==3489== Possible data race during write of size 8 at 0x4003330 by thread #1
==3489== Locks held: none
==3489== at 0x42970F: _int_free (in /h/d9/b/dat11ote/courses/edan25/lab4home/live)
==3489== by 0x402D5C: liveness (paralleldataflow.c:244)
==3489== by 0x401E4F: main (main.c:134)
==3489==
==3489== This conflicts with a previous read of size 8 by thread #2
==3489== Locks held: none
==3489== at 0x402C4C: thread_start (paralleldataflow.c:155)
==3489== by 0x4040B1: start_thread (pthread_create.c:312)
==3489== by 0x4500E8: clone (in /h/d9/b/dat11ote/courses/edan25/lab4home/live)
来自25个上下文的30多个错误。如果我将return语句更改为在
中的线程参数之前void* thread_start(void* arguments)
{
size_t index;
return NULL;
}
然后一切正常。我使用-pthreads和-static标志来gcc。如果我删除printf和fflush,这会留下上面的错误,但会删除所有其他错误,如下所示:
Possible data race during write of size 8 at 0x6D7878 by thread #1
Locks held: none
at 0x40F449: vfprintf (in /h/../live)
by 0x419075: printf (in /h/../live)
by 0x401E76: main (main.c:137)
This conflicts with a previous write of size 8 by thread #2
Locks held: none
at 0x40F449: vfprintf (in /h/../live)
by 0x419075: printf (in /h/../live)
by 0x402C68: thread_start (in /h/../live)
by 0x404061: start_thread (pthread_create.c:312)
by 0x44B2A8: clone (in /h/../live)
答案 0 :(得分:3)
如果使用-static进行链接,则表示valgrind / helgrind 不能替换或包装必须更换/包装的一组功能 让helgrind正常工作。
通常,为了让helgrind正常工作,诸如malloc / free / ...之类的功能 必须更换。 像pthread_create / pthread_join / ...这样的函数必须用helgrind包装。
使用静态库意味着不会替换或包装这些函数, 引起很多误报。