线程结束后同步
因为这个问题的一部分没有得到解决,我'我将其作为一个单独的问题:
#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;
}
这个程序是否没有数据竞争?
对于新的 C++ 内存模型,是否可以保证线程 t2
和线程 main
对变量 ret
的访问是同步的?
我的意思是,如果程序在同一内核上执行,那么来自 t2
和 main
的访问显然不会发生冲突。
但是如果 t2
和 main
在不同的内核上执行怎么办?
在 main
继续执行之前,是否可以保证核心缓存同步?
如果有人可以提供相同的参考资料,我将不胜感激。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的程序没有数据竞争。 [thread.thread.member]/p5 用同步段落描述 join():
同步: *this 表示的线程完成同步
与 (1.10) 相应的 join() 成功返回。 [注意:*this 上的操作不同步。 —endnote]
(1.10) 指的是 [intro.multithread],该部分太长,无法引用,但详细定义了短语“同步”。
最新的工作草案是 N3225。
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.