我使用 Valgrind
的 helgrind
工具测试了一个应用程序的竞争条件。但它报告了对受锁保护的代码的竞争。是 Valgrind
错误地将此报告为竞争条件,还是我遗漏了什么。代码如下。
pthread_mutex_lock(&G_Memory->lock_array[pb->exp_lock_index]);
pb->subtree_cost += b->subtree_cost;
pb->interaction_synch += 1; // <--- race here (cost_zones.c:91)
pthread_mutex_unlock(&G_Memory->lock_array[pb->exp_lock_index]);
Valgrind/Helgrind
报告如下
==29768== Possible data race during read of size 8 at 0x56bf8e0 by thread #4
==29768== at 0x404C51: ComputeSubTreeCosts (cost_zones.c:91)
...................
==29768== This conflicts with a previous write of size 8 by thread #1
==29768== at 0x404C5F: ComputeSubTreeCosts (cost_zones.c:91)
...................
最佳答案
根据您在评论中的描述,您遗漏了一些东西。你在这里有一个竞争条件,因为线程使用不同的互斥体。您的线程必须在此处使用相同的互斥锁,以便代码仅在它们获得唯一锁时执行。
关于c++ - Valgrind 在不应该的时候报告竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15251670/