[英]Alternatives to refcount for unique_ptr
In following code example, there should be exist one instance of struct A
inside struct B
as long as any object of B
is living. 在下面的代码示例,应该有存在结构的一个实例
A
结构内部B
只要任何对象B
住。 Example works as expected. 示例按预期工作。
#include <memory>
#include <iostream>
#include <mutex>
struct A
{
A() { std::cout << "A() called" << std::endl; }
~A() { std::cout << "~A() called" << std::endl; }
};
struct B
{
B()
{
std::cout << "B() called" << std::endl;
std::lock_guard<std::mutex> guard(mtx);
if( !refCount )
{
a.reset( new A );
}
++refCount;
}
~B()
{
std::cout << "~B() called" << std::endl;
std::lock_guard<std::mutex> guard(mtx);
--refCount;
if( !refCount )
{
a.reset( );
}
}
static std::unique_ptr<A> a;
static std::mutex mtx;
static int refCount;
};
std::unique_ptr<A> B::a;
int B::refCount(0);
std::mutex B::mtx;
int main()
{
{
B b1; //B::a should be created here
B b2;
} //B::a should be destroyed here
B b3; // B::a should be recreated here
} //B::a should be destroyed again here
See also http://coliru.stacked-crooked.com/a/fea428254933ee5c 另见http://coliru.stacked-crooked.com/a/fea428254933ee5c
My question: Is there an alternative (threadsafe!) implementation without a refcount? 我的问题:是否有一个没有refcount的替代(线程安全!)实现? Is this maybe possible to solve with a construct of
std::shared_ptr
and std::weak_ptr
? 这可能是用
std::shared_ptr
和std::weak_ptr
构造解决的吗?
The only way to ensure that an object lives "as long as any object of B
is living" is by keeping a refcount of B
objects. 确保对象“只要
B
任何对象生存”的唯一方法是保留B
对象的引用计数。 It's the only realistic way to tell if there are any living B
objects, if they are going to be arbitrarily created and destroyed as the program runs. 这是判断是否存在任何
B
对象的唯一现实方法,如果它们随着程序的运行而被随意创建和销毁。
std::shared_ptr
internally keeps refcounts, which are tracked atomically. std::shared_ptr
内部保持refcounts,这是自动跟踪的。 It's probably a better idea to use those, rather than manually managing the refcount yourself; 使用它们可能是更好的主意,而不是自己手动管理引用计数; that way you don't have to meticulously implement all of the RAII constructors, or reinvent the wheel.
这样你就不必一丝不苟地实现所有RAII构造函数,或重新发明轮子。
Here is the solution wanted: 这是想要的解决方案:
#include <memory>
#include <iostream>
#include <mutex>
using std::shared_ptr;
using std::weak_ptr;
struct A
{
A() { std::cout << "A() called" << std::endl; }
~A() { std::cout << "~A() called" << std::endl; }
};
struct B
{
B()
{ std::cout << "B() called" << std::endl;
std::lock_guard<std::mutex> guard(mtx);
if (!(ac =aw.lock()))
aw =ac =shared_ptr<A>(new A);
}
~B()
{ std::cout << "~B() called" << std::endl;
}
shared_ptr<A> ac;
static weak_ptr<A> aw;
static std::mutex mtx;
};
weak_ptr<A> B::aw;
std::mutex B::mtx;
int main()
{
{
B b1; //B::a should be created here
B b2;
} //B::a should be destroyed here
B b3; // B::a should be recreated here
} //B::a should be destroyed again here
which produces the same output as your example: 它产生与您的示例相同的输出:
B() called
A() called
B() called
~B() called
~B() called
~A() called
B() called
A() called
~B() called
~A() called
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.