【发布时间】:2019-06-18 21:06:11
【问题描述】:
编译器能否在多线程程序上下文中优化静态存储对象?例如,我要求它知道声明为静态的变量在用于线程中调用的函数时是否会产生副作用。
bool flag = false; // static storage duration object
void f(){ //function called in a thread
flag = false;
// do some work...
flag = true;
}
//a possible representation of the code above after optimization
void f(){
flag = true;
// do some work...
} // is this possible to happen?
我从here 阅读了一些答案,但没有找到任何可以帮助的信息。
【问题讨论】:
-
即使标准会保证从静态存储持续时间对象中分配和读取将被视为副作用,它也必须影响线程安全。线程安全是完全不同的野兽。
标签: c++ multithreading c++11 storage-duration