什么是潜在的共享内存位置?
What is a potentially shared memory location?
在C11和C++11标准中出现了语句potentially shared memory location。这是什么意思?是否所有全局变量都可能在多线程环境中共享?
对C标准不是很熟悉。在 C++14 中,“potentially shared memory location”这个短语出现了两次,分别出现在两个非规范注释中:
[intro.multithread]/25 [ Note: Compiler transformations that introduce assignments to a potentially shared memory location that would not be modified by the abstract machine are generally precluded by this standard, since such an assignment might overwrite another assignment by a different thread in cases in which an abstract machine execution would not have encountered a data race. This includes implementations of data member assignment that overwrite adjacent members in separate memory locations. Reordering of atomic loads in cases in which the atomics in question may alias is also generally precluded, since this may violate the coherence rules. — end note ]
[intro.multithread]/26 [ Note: Transformations that introduce a speculative read of a potentially shared memory location may not preserve the semantics of the C++ program as defined in this standard, since they potentially introduce a data race. However, they are typically valid in the context of an optimizing compiler that targets a specific machine with well-defined semantics for data races. They would be invalid for a hypothetical machine that is not tolerant of races or provides hardware race detection. — end note ]
从上下文来看,很明显“潜在共享内存位置”应该是指“优化器不能排除其他线程可能访问它的可能性的内存位置,因此应该继续进行悲观假设他们可能会。”然后,这两个注释讨论了在这种假设下可能会或可能不会进行的某些优化的合法性。
回复:全局变量。是的,全局变量通常可以被任意线程访问。原则上可以想象,执行整个程序优化的复杂优化器可能能够证明一个特定的全局变量永远不会从多个线程并发访问(我不知道目前存在的任何实际编译器能够实现这样的壮举,但无论如何我都不会称自己为编译器专家)。除此之外,全局变量占用的内存位置应该被优化器视为潜在共享。
在C11和C++11标准中出现了语句potentially shared memory location。这是什么意思?是否所有全局变量都可能在多线程环境中共享?
对C标准不是很熟悉。在 C++14 中,“potentially shared memory location”这个短语出现了两次,分别出现在两个非规范注释中:
[intro.multithread]/25 [ Note: Compiler transformations that introduce assignments to a potentially shared memory location that would not be modified by the abstract machine are generally precluded by this standard, since such an assignment might overwrite another assignment by a different thread in cases in which an abstract machine execution would not have encountered a data race. This includes implementations of data member assignment that overwrite adjacent members in separate memory locations. Reordering of atomic loads in cases in which the atomics in question may alias is also generally precluded, since this may violate the coherence rules. — end note ]
[intro.multithread]/26 [ Note: Transformations that introduce a speculative read of a potentially shared memory location may not preserve the semantics of the C++ program as defined in this standard, since they potentially introduce a data race. However, they are typically valid in the context of an optimizing compiler that targets a specific machine with well-defined semantics for data races. They would be invalid for a hypothetical machine that is not tolerant of races or provides hardware race detection. — end note ]
从上下文来看,很明显“潜在共享内存位置”应该是指“优化器不能排除其他线程可能访问它的可能性的内存位置,因此应该继续进行悲观假设他们可能会。”然后,这两个注释讨论了在这种假设下可能会或可能不会进行的某些优化的合法性。
回复:全局变量。是的,全局变量通常可以被任意线程访问。原则上可以想象,执行整个程序优化的复杂优化器可能能够证明一个特定的全局变量永远不会从多个线程并发访问(我不知道目前存在的任何实际编译器能够实现这样的壮举,但无论如何我都不会称自己为编译器专家)。除此之外,全局变量占用的内存位置应该被优化器视为潜在共享。