Current state of drd and helgrind support for std::thread

一世执手 提交于 2019-11-30 18:13:06

std::thread uses a shared pointer internally. What you are seeing are false positives on the reference count of that shared pointer object. You can avoid these false positives by adding the four lines of code shown below in each source file just before the C++ header include directives. Note: this only works with the version of libstdc++ included with gcc 4.6.0 or later.

#include <valgrind/drd.h>
#define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(addr) ANNOTATE_HAPPENS_BEFORE(addr)
#define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(addr) ANNOTATE_HAPPENS_AFTER(addr)
#define _GLIBCXX_EXTERN_TEMPLATE -1

For more information, see also the Data Race Hunting section in the libstdc++ manual (http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug.html).

Most likely what you are seeing are false positives. I am observing a similar behaviour in my code.

Specifically, the warnings seem related to the implementation of the shared pointer class, and my understanding is that on your platform (which I presume is x86/x86-64?) GCC is using optimized atomic assembly instruction in the shared pointer reference counting machinery. The problem is that valgrind is able to detect errors when using the POSIX primitives (locks, mutexes, etx.), but it is not able to cope with lower level primitives.

What I've done so far is to simply filter out from the output of valgrind the warnings (possibly you could write some suppression file that does the job in the proper way).

If you use boost you can turn on the use of pthreads primitives instead of atomic operations for shared pointers. You can then use a version of your code compiled with BOOST_SP_USE_PTHREADS for helgrind analysis, and you will not get the errors because helgrind understands the pthreads primitives.

See http://www.boost.org/doc/libs/1_54_0/libs/smart_ptr/shared_ptr.htm#ThreadSafety for more info.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!