3

I am using Clang-8 with thread sanitizer enabled to compile the code below

std::atomic<std::string*> ptr {nullptr};
int data {0};

void producer() {
  std::string* p  = new std::string("Hello");
  data = 42; 
  ptr.store(p, std::memory_order_release);
}

void consumer() {
  std::string* p2; 
  if(!(p2 = ptr.load(std::memory_order_relaxed))) {
     // Data is not ready, just return
     return ;
  }
  std::atomic_thread_fence(std::memory_order_seq_cst);
  assert(data == 42); // Never fired
}

int main() {
  std::thread t1(producer);
  std::thread t2(consumer);
  t1.join();   t2.join();
}

Thread sanitizer reports a data race warning on the global variable data. Why this is considered a data race?

==================
WARNING: ThreadSanitizer: data race (pid=20388)
  Read of size 4 at 0x000001191718 by thread T2:
    #0 consumer() <null> (a.out+0x4afe32)
    #1 void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) <null> (a.out+0x4b1fad)
    #2 std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) <null> (a.out+0x4b1ee0)
    #3 decltype(std::__invoke(_S_declval<0ul>())) std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) <null> (a.out+0x4b1e88)
    #4 std::thread::_Invoker<std::tuple<void (*)()> >::operator()() <null> (a.out+0x4b1e28)
    #5 std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() <null> (a.out+0x4b1c1c)
    #6 <null> <null> (libstdc++.so.6+0xbd66e)

  Previous write of size 4 at 0x000001191718 by thread T1:
    #0 producer() <null> (a.out+0x4afcca)
    #1 void std::__invoke_impl<void, void (*)()>(std::__invoke_other, void (*&&)()) <null> (a.out+0x4b1fad)
    #2 std::__invoke_result<void (*)()>::type std::__invoke<void (*)()>(void (*&&)()) <null> (a.out+0x4b1ee0)
    #3 decltype(std::__invoke(_S_declval<0ul>())) std::thread::_Invoker<std::tuple<void (*)()> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) <null> (a.out+0x4b1e88)
    #4 std::thread::_Invoker<std::tuple<void (*)()> >::operator()() <null> (a.out+0x4b1e28)
    #5 std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)()> > >::_M_run() <null> (a.out+0x4b1c1c)
    #6 <null> <null> (libstdc++.so.6+0xbd66e)

  Location is global 'data' of size 4 at 0x000001191718 (a.out+0x000001191718)

  Thread T2 (tid=20391, running) created by main thread at:
    #0 pthread_create <null> (a.out+0x424775)
    #1 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) <null> (libstdc++.so.6+0xbd924)
    #2 main <null> (a.out+0x4afec1)

  Thread T1 (tid=20390, finished) created by main thread at:
    #0 pthread_create <null> (a.out+0x424775)
    #1 std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) <null> (libstdc++.so.6+0xbd924)
    #2 main <null> (a.out+0x4afeae)

SUMMARY: ThreadSanitizer: data race in consumer()
==================
ThreadSanitizer: reported 1 warnings

Update

I have read the Why does ThreadSanitizer report a race with this lock-free example? and compile the code using Clang-8. It does not show any data race warning. So I think my case is different.

Tes
  • 349
  • 3
  • 12

1 Answers1

0

Your example has no synchronization between the store of 42 into data and the write to the value of data. The compiler is free to re-order these two in producer, which means the value of data is undefined even after the barrier and the check.

Mark Pauley
  • 1,445
  • 12
  • 11