Thread: helgrind shows race conditions with atomic types

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    13

    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 : atomic::atomic - C++ Reference in helgrind:


    Code:
    valgrind --tool=helgrind ./main

    Then helgrind shows this error :


    Code:
    ==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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Helgrind is a Valgrind tool for detecting synchronisation errors in C, C++ and Fortran programs that use the POSIX pthreads threading primitives.
    Helgrind only works on the POSIX threads API.
    If your library achieves synchronisation by other means, helgrind will report false information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    13
    Hi Salem,

    You are right. If I use mutex instead of c++ atomic types, Helgrind does not report any error at all.

    thanks a lot!!

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    13
    By the way, How can I tag this post as Solved?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You just did.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2015
    Posts
    13
    By the way,

    If you use ready.exchange(true) instead of ready = true on line 20, helgrind shows no errors at all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-24-2014, 11:47 AM
  2. Replies: 4
    Last Post: 01-23-2014, 01:01 AM
  3. How is it possible for valgrind to prevent race conditions?
    By MutantJohn in forum C++ Programming
    Replies: 14
    Last Post: 11-23-2013, 10:14 PM
  4. Atomic Types
    By miclus in forum C++ Programming
    Replies: 4
    Last Post: 01-01-2004, 08:39 PM
  5. Atomic instructions
    By Roaring_Tiger in forum C Programming
    Replies: 1
    Last Post: 04-29-2003, 09:00 PM

Tags for this Thread