我有一个在多个线程中调用的打印到标准输出的函数,所以我在它上面加了一个锁。问题是尽管互斥锁应该被锁定,但 helgrind 会在保存指向 stdout 的指针的变量上报告 RAC。我的代码如下所示:
#ifdef ___USE_DBG
#define MSG(message) f(message)
#else
#define MSG(message)
#endif
void f(message)
{
lock(&m1); //lock first observed
out<<message; //locks held 2 . why? m1 is the only mutex....
unlock(&m1);
}
我用一些调试消息在不同的线程中调用 MSG。显然,当我不使用调试版本时,我不会收到消息,因为该函数会从程序中消失。我尝试调试并在 m1._数据上设置条件断点。_lock!=0 但没有触发。调试此问题的正确方法是什么。
变量名和实际函数内容不同。
此外,可连接线程和已分离线程都调用函数。
我有 5 个线程持有/等待互斥锁 m2 和另外 5 个在 m3 上,每个线程都在锁定区域内调用 MSG(mess)。这里的比赛条件是什么?每个人不应该因为 m1 而获得自己的味精吗?
type1_thread{
<loop everything until something happens>
lock(&m2);
...
MSG(something);
unlock(&m2);
}
type2_thread{
<loop everything until something happens>
lock(&m3);
...
MSG(something_else);
unlock(&m3);
}
<start x type1 threads>
<start y type2 threads>
如果需要进一步澄清,我将进行编辑。实际代码在 Intranet 上并且更大。