[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gcc-bugs
Subject:    [Bug c++/103878] New: ThreadSanitizer: false report about data race
From:       fchelnokov at gmail dot com via Gcc-bugs <gcc-bugs () gcc ! gnu ! org>
Date:       2021-12-31 15:28:12
Message-ID: bug-103878-4 () http ! gcc ! gnu ! org/bugzilla/
[Download RAW message or body]

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103878

            Bug ID: 103878
           Summary: ThreadSanitizer: false report about data race
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

The program as follows is valid:
```
#include <atomic>
#include <iostream>
#include <future>

int state = 0;
std::atomic_int a = 0;

void foo(int from, int to) 
{
    for (int i = 0; i < 10; i++)
    {
        while (a.load(std::memory_order_relaxed) != from) {}
        std::atomic_thread_fence(std::memory_order_acquire);
        state++;
        a.store(to, std::memory_order_release);
    }
}

int main()
{    
    auto x = std::async(std::launch::async, foo, 0, 1);
    auto y = std::async(std::launch::async, foo, 1, 0);
}
```
The access to `state` variable is not a data race, because each thread before
the modification executes `atomic_thread_fence` to see the results of the other
thread, and after the modification executes atomic store with
`memory_order_release`. But the sanitizer erroneously reports data race. Demo:
https://gcc.godbolt.org/z/9cY3aM3cz

Related discussion: https://stackoverflow.com/q/70542993/7325599=
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic