赫尔格林德和atomic_flag

Helgrind and atomic_flag

本文关键字:atomic flag 林德      更新时间:2023-10-16

我在 cplusplus.com 尝试了使用atomic_flag的基本示例。瓦尔格林德的赫尔格林德工具报告

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作为旋转锁的参考是错误的,还是赫尔格林德在这里给出了误报?

这些是误报。赫尔格林德不理解"低级"同步, 它只理解 POSIX 同步原语。

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

确保您的应用程序及其使用的所有库都使用 POSIX 线程原语。赫尔格林德需要能够看到所有内容 与线程创建、退出、锁定和其他相关的事件 同步事件。为此,它会拦截许多 POSIX pthread。 功能。

不要从中滚动自己的线程原语(互斥体等( Linux futex 系统调用、原子计数器等的组合。这些 将赫尔格林德的内部正在发生的事情模型抛出轨道, 会给出虚假的结果。

因此,等待 helgrind 理解例如 c++ 原子标志,您将拥有 使用客户端请求注释代码,让 Helgrind "看到"的 基于原子标志的同步基元。