0
假设我有以下代码:缓存不变的价值:避免锁
struct Obj {
mutable bool valueIsCached_;
mutable int value_;
const int parameter_;
Obj (int parameter) : valueIsCached_ (false), parameter_ (parameter) {
}
int getValue() const {
if (!valueIsCached) {
value_ = calculateValue(); // #1
valueIsCached_ = true; // #2
}
return value_;
}
private:
int calculateValue() {
/* calculate value based on parameter and return it;
return value only depends on value of parameter_
(no side effects; no dependence on global state)
*/
}
};
这段代码,很明显,线程安全的,如果编译器不重新排序线标记#1和#2。可能发生的最糟糕的事情是多重计算value_
。但是,只要我们无法保证#2在#1之后发生,代码就会线程不安全。
- 我们怎样才能避免这种情况?
- 做事情变得更糟如果
value_
类型是,例如,一个struct
?
它不是在重新排序书面线程安全的,不管。 –
。试着想象最坏的情况下有两个线程A和B“同时”呼叫'的getValue()':假设一个刚刚执行'如果(valueIsCached!)'和系统决定切换到线程B拥有的方式更高的优先级和执行直到返回value_。现在A可以再次运行,但损坏已经完成。的 – stijn
可能重复[延迟初始化缓存...我如何使它线程安全的?](http://stackoverflow.com/questions/8097439/lazy-initialized-caching-how-do-i-make-it-线程安全) – stijn