c++ - std::thread::join是否保证写入可见性

标签 c++ multithreading language-lawyer

据说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?

最佳答案

[thread.thread.member]:

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 successful join() 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/

相关文章:

c++ - C++ 中的指针和引用

c++ - 函数中的 vector - 如何返回

java - Thread.sleep 等待时间超过预期

c++ - C++十进制类型和括号-为什么?

c++ - 在参数中移出的智能指针上调用方法是否安全?

c++ - 如何隐藏 libfreenect2 的输出消息?

c++ - 使用多线程时性能几乎没有提高

java - 将线程注册到 Phaser

java - 创建线程对象后调用实现runnable的Java类的方法

c++ - 这个重载决议是否正确?