1

Because part of this question wasn't addressed, I'm making it a separate question:

#include<iostream>
#include<thread>
using namespace std;

void f2(double* ret) {
   *ret=5.;
}

int main() {
   double ret=0.;
   thread t2(f2, &ret);
   t2.join();
   cout << "ret=" << ret << endl;   
}

Is this program data race free?
Are there any guarantees, with respect to new C++ memory model, that accesses to variable ret from thread t2 and thread main are synchronized?

I mean, it is obvious that accesses from t2 and main won't collide if the program is executed on the same core.
But what if t2 and main are executed on different cores?
Are there any guarantees that core's caches will synchronize before main continues execution?

I'd appreciate if somebody could provide same references.

Thank you.

Community
  • 1
  • 1
Predrag
  • 1,527
  • 2
  • 13
  • 17

1 Answers1

5

Your program is data race free. [thread.thread.member]/p5 describes join() with a Synchronization paragraph:

Synchronization: The completion of the thread represented by *this synchronizes with (1.10) the corresponding successful join() return. [Note: Operations on *this are not synchronized. —endnote]

(1.10) refers to [intro.multithread] which is a section too long to quote but defines in excruciating detail the phrase "synchronizes with".

The latest working draft is N3225.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577