据说Std::thread::join与所连接的线程“同步”,但是同步并不能说明副作用的可见性,它仅控制可见性的顺序,即。在以下示例中:
int g_i = 0;
int main()
{
auto fn = [&] {g_i = 1;};
std::thread t1(fn);
t1.join();
return g_i;
}
在c++标准中,我们是否对保证此程序将始终返回1?
最佳答案
void join();
Effects: Blocks until the thread represented by*this
has completed.
Synchronization: The completion of the thread represented by*this
synchronizes with the corresponding successfuljoin()
return.
由于线程执行的完成与
thread::join
的返回值同步,因此线程inter-thread happens before的返回完成了:An evaluation A inter-thread happens before an evaluation B if
— A synchronizes with B
因此happens before它:
An evaluation A happens before an evaluation B (or, equivalently, B happens after A) if:
— A inter-thread happens before B
由于(线程间)发生在传递性之前(让我跳过复制以粘贴整个线程间定义来显示此行为),因此在线程完成之前发生的所有事情,包括将值
1
写入g_i
,都会发生从thread::join
返回之前。继而从thread::join
返回的结果发生在读取g_i
中的return g_i;
的值之前,这仅仅是因为thread::join
的调用在return g_i;
之前进行了排序。再次,使用可传递性,我们确定在非主线程中将1
写入g_i
的操作发生在主线程中g_i
中的return g_i;
的读取之前。对于
1
中的g_i
的读取,将g_i
写入return g_i;
是visible side effect:A visible side effect A on a scalar object or bit-field M with respect to a value computation B of M satisfies the conditions:
— A happens before B and
— there is no other side effect X to M such that A happens before X and X happens before B.
The value of a non-atomic scalar object or bit-field M, as determined by evaluation B, shall be the value stored by the visible side effect A.
最后一句话的重点是我的,它保证了从
g_i
中的return g_i;
读取的值将是1
。
关于c++ - std::thread::join是否保证写入可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65515727/