我尝试了在cplusplus.com上使用atomic_flag的基本示例。 Valgrind的Helgrind工具报告

164 errors from 28 contexts (suppressed: 0 from 0)

例如
==4868== Possible data race during read of size 1 at 0x605220 by thread #3
==4868== Locks held: none
==4868==    at 0x401172: test_and_set (atomic_base.h:176)
==4868==    by 0x401172: append_number(int) (helgrind_spinlock.cpp:12)
[output deleted]
==4868== This conflicts with a previous write of size 1 by thread #2
==4868== Locks held: none
==4868==    at 0x4011C9: clear (atomic_base.h:193)
==4868==    by 0x4011C9: append_number(int) (helgrind_spinlock.cpp:14)
[output deleted]

正确使用atomic_flag作为自旋锁时,引用是否错误?或者Helgrind在此处给出误报吗?

最佳答案

这些都是假阳性。 Helgrind不了解“低级”同步,
它仅了解posix同步原语。

请参见http://www.valgrind.org/docs/manual/hg-manual.html#hg-manual.effective-use用户手册,例如说:



因此,等待Helgrind了解例如c++原子标志,您将拥有
用客户请求注释代码,让helgrind“看到”
基于原子标记的同步原语。

关于c++ - Helgrind和atomic_flag,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52488939/

10-11 00:57