【发布时间】:2021-10-26 08:46:27
【问题描述】:
标准 C++ 库中是否有线程安全的引用计数器类(或作为 Visual Studio 中的扩展),还是我需要从头开始编写这种对象?
我希望有一个像shared_ptr 那样纯粹执行引用计数的对象,但它可以跨多个线程准确地执行此操作,并且无需管理任何东西。 shared_ptr 它的表亲结构很好,因为它们定义了您需要的所有复制构造函数和赋值运算符,这对我来说是 C++ 中最容易出错的部分; C++ 构造函数之于 C++ 就像开球之于美式足球一样。
struct Fun {
// this member behaves in a way I appreciate, save for 2 short-comings:
// - needless allocation event (minor)
// - ref counting is only estimate if shared across threads (major)
std::shared_ptr<int> smartPtr {new int};
// this is the hypothetical object that I'm searching for
// + allocates only a control block for the ref count
// + refCount.unique() respects reality when refs exist across many threads
// I can always count on this being the last reference
std::object_of_desire refCount;
// no explicit copy constructors or assignment operators necessary
// + both members of this class provide this paperwork for me,
// so I can careless toss this Fun object around and it'll move
// as one would expect, making only shallow copies/moves and ref counting
Fun();
~Fun(){
if(refCount.unique()){
smart_assert("I swear refCount truly is unique, on pain of death");
}
}
}
【问题讨论】:
-
我不明白你的问题。为什么你不想使用 shared_ptr?
-
这能回答你的问题吗? std::shared_ptr thread safety explained
-
如果您不能将
refCount.unique()锁定为线程安全值,那么它的目的是什么?在您调用 unique 和调用smart_assert之间可能会增加 -
shared_ptr进行线程安全引用计数,如果您将其设置为不管理任何内容,它就无法管理任何内容。所以没有必要重新发明轮子。 -
@AnneQuinn 在您调用
unique之后,是什么阻止了另一个线程复制最后一个引用?