参考返回值我有一个非常相似的问题已经问2012年临界区,并在C++
Critical Sections and return values in C++
我想访问一个容器线程安全的藏汉而是通过引用返回缓存的版本。
struct Container {
const Data& getSomeData() const {
EnterCriticalSection(& myCritSec);
if (update) {
cache.calulatefromcontainer();
}
// fill retobj with data from structure
LeaveCriticalSection(& myCritSec);
return cache;
}
private:
mutable Data cache;
};
问题是,“返回缓存”行不再受到保护。是否有可能通过引用返回“缓存”线程安全?
返回对可修改数据的引用基本上是对线程安全容器的禁用。这里并没有多少意思。 – SergeyA