我在 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例如说:
Make sure your application, and all the libraries it uses, use the POSIX threading primitives. Helgrind needs to be able to see all events pertaining to thread creation, exit, locking and other synchronisation events. To do so it intercepts many POSIX pthreads functions.
Do not roll your own threading primitives (mutexes, etc) from combinations of the Linux futex syscall, atomic counters, etc. These throw Helgrind's internal what's-going-on models way off course and will give bogus results.
因此,等待 helgrind 理解,例如c++原子标志,你将拥有 用客户端请求注释代码让 helgrind “看到” 基于原子标志的同步原语。
关于c++ - Helgrind 和 atomic_flag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488939/