helgrind shows race conditions with atomic types

Hi there!

Valgrind tool helgrind is reporting some race conditions with atomic variables.

If you run the code in this link : http://www.cplusplus.com/reference/atomic/atomic/atomic/ in helgrind:

 
valgrind --tool=helgrind ./main


Then helgrind shows this error :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
==14825== Possible data race during write of size 1 at 0x6062C9 by thread #1
==14825== Locks held: none
==14825==    at 0x4015B3: store (atomic_base.h:374)
==14825==    by 0x4015B3: std::__atomic_base<bool>::operator=(bool) (atomic_base.h:267)
==14825==    by 0x40130E: std::atomic<bool>::operator=(bool) (atomic:74)
==14825==    by 0x40119F: main (main.cpp:21)
==14825== 
==14825== This conflicts with a previous read of size 1 by thread #11
==14825== Locks held: none
==14825==    at 0x401343: load (atomic_base.h:396)
==14825==    by 0x401343: std::atomic<bool>::operator bool() const (atomic:81)
==14825==    by 0x401097: count1m(int) (main.cpp:11)
==14825==    by 0x4037BB: void std::_Bind_simple<void (*(int))(int)>::_M_invoke<0ul>(std::_Index_tuple<0ul>) (functional:1531)
==14825==    by 0x4036C7: std::_Bind_simple<void (*(int))(int)>::operator()() (functional:1520)
==14825==    by 0x403657: std::thread::_Impl<std::_Bind_simple<void (*(int))(int)> >::_M_run() (thread:115)
==14825==    by 0x4EF8C7F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==14825==    by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==14825==    by 0x53DF6B9: start_thread (pthread_create.c:333)
==14825==  Address 0x6062c9 is 0 bytes inside data symbol "ready"


Why? Is not the atomic boolean 'ready' supposed to be thread-safe?

Thanks a lot!

All the best
Last edited on
Helgrind does not understand standard C++ (or for that matter C11) memory order relationships. It assumes that the program uses the crude (and by now thoroughly obsolete) concurrency primitives as specified by POSIX.
(Helgrind is also somewhat ignorant about POSIX condition variables.)

Either ignore the spurious warnings or annotate your program to educate helgrind about memory order relationships imposed by the atomic operations library.
See ANNOTATE_HAPPENS_AFTER and friends:
http://valgrind.org/docs/manual/hg-manual.html#hg-manual.effective-use
#include <helgrind.h> http://repo.or.cz/valgrind.git/blob/refs/heads/master:/helgrind/helgrind.h
Registered users can post here. Sign in or register to post.