C/C++教程

C++ happens-before 关系是不可传递的

本文主要是介绍C++ happens-before 关系是不可传递的,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

P0668R4 对此进行了解释

The definition of plain happens-before became unpleasantly complicated with the introduction of memory_order_consume. And it is not transitive, which remains counterintuitive. This proposal changes neither of those. And if the user refrains from using memory_order_consume it can continue to be entirely ignored, as before. Until we have a usable version of memory_order_consume, I would expect teaching materials to ignore these issues, and pretend that happens-befors is defined as our simply-happens-before relation, which is clearly transitive. In the presence of memory_order_consume, this problem is unavoidable, since consume can order two accesses without also ordering the first with respect to an access that immediately follows the second; happens-before cannot compose with sequenced-before, and thus happens-before cannot be transitive.

在不使用 memory_order_consume 的情况下,happens-before 可以视为可传递,否则为不可传递。C++17 提出了 strongly happens before,排除了 memory_order_consume,是可传递的,可以和 sequenced-before 进行组合。

这篇关于C++ happens-before 关系是不可传递的的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!