简体   繁体   English

线程结束后进行同步

[英]Synchronization after thread end

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? 对于新的C ++内存模型,是否可以保证从线程t2和线程main对变量ret访问是同步的?

I mean, it is obvious that accesses from t2 and main won't collide if the program is executed on the same core. 我的意思是,如果程序在同一内核上执行,显然t2main访问不会冲突。
But what if t2 and main are executed on different cores? 但是,如果t2main在不同的内核上执行怎么办?
Are there any guarantees that core's caches will synchronize before main continues execution? 是否可以保证在main继续执行之前内核的缓存会同步?

I'd appreciate if somebody could provide same references. 如果有人可以提供相同的参考,我将不胜感激。

Thank you. 谢谢。

Your program is data race free. 您的程序没有数据争用。 [thread.thread.member]/p5 describes join() with a Synchronization paragraph: [thread.thread.member] / p5用Synchronization段落描述了join():

Synchronization: The completion of the thread represented by *this synchronizes with (1.10) the corresponding successful join() return. 同步:* this表示的线程的完成与(1.10)相应的成功join()返回同步。 [Note: Operations on *this are not synchronized. [注意:*此操作不同步。 —endnote] --endnote]

(1.10) refers to [intro.multithread] which is a section too long to quote but defines in excruciating detail the phrase "synchronizes with". (1.10)指的是[intro.multithread],该节太长了,无法引用,但详细定义了短语“与...同步”。

The latest working draft is N3225 . 最新的工作草案是N3225

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM