c++ - Valgrind 在不应该的时候报告竞争条件

标签 c++ c linux valgrind race-condition

我使用 Valgrindhelgrind 工具测试了一个应用程序的竞争条件。但它报告了对受锁保护的代码的竞争。是 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/

相关文章:

c++ - OpenCV 函数中的内存泄漏 : cvQueryFrame()

c++ - any 类中的模板构造函数与非模板构造函数

c++ - 复制 C 字符串的正确方法

linux - 在文件中使用 sed 替换复杂行

c++ - 关于 VS 2010 中用于 C++ 的代码覆盖工具

c - Valgrind:C 程序中肯定丢失内存泄漏

c - 将字符串中的值替换为另一个字符串中的另一个值

c - tm 结构和 MPLABX x8 编译器

linux - 关于内核中的 CONFIG_NO_HZ

linux - TCP 接收窗口大小高于 net.core.rmem_max