如何从缓存中删除智能指针时,有没有更多的参考? [英] How to remove smart pointers from a cache when there are no more references?

查看:130
本文介绍了如何从缓存中删除智能指针时,有没有更多的参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用智能指针来升级现有的应用程序,我努力克服一个难题。在我的应用我有对象的缓存,例如让他们打电话的书籍。现在的图书这个缓存都按ID请求,如果他们在缓存它们返回,如果没有物体从一个外部系统(缓慢操作)请求并添加到高速缓存。一旦在高速缓存中的许多窗口可以在应用程序中打开,这些窗口都可以取到书的参考。在应用程序中的previous版本程序员不得不维持AddRef和释放,在使用Book对象的每个窗口被关闭,最终发行(在高速缓存管理器)会从缓存中删除对象并删除对象。

I've been trying to use smart pointers to upgrade an existing app, and I'm trying to overcome a puzzle. In my app I have a cache of objects, for example lets call them books. Now this cache of books are requested by ID and if they're in the cache they are returned, if not the object is requested from an external system (slow operation) and added to the cache. Once in the cache many windows can be opened in the app, each of these windows can take a reference to the book. In the previous version of the app the programmer had to maintain AddRef and Release, when every window using the Book object was closed, the final Release (on the cache manager) would remove the object from the cache and delete the object.

您可能已经发现在这里链中的薄弱环节,这是当然的程序员记住调用AddRef和Release。现在,我已经转移到智能指针(升压::侵入)我再也不用担心调用AddRef和Release。然而,这将导致一个问题,缓存具有对对象的引用,所以当关闭最后一个窗口,缓存不通知任​​何人都不是抱着一个参考。

You may have spotted the weak link in the chain here, it is of course the programmer remembering to call AddRef and Release. Now I have moved to smart pointers (boost::intrusive) I no longer have to worry about calling AddRef and Release. However this leads to a problem, the cache has a reference to the object, so when the final window is closed, the cache is not notified that no-one else is holding a reference.

我的第一个想法是定期走高速缓存和清除的对象一个的引用计数。我不喜欢这个主意,因为它是一个n阶操作,并没有觉得不对劲。我想出了一个回调系统,这是更好,但不是太棒了。我已经包括了code回调系统,但是我想知道如果任何人有这样做的更好的办法?

My first thoughts were to periodically walk the cache and purge objects with a reference count of one. I didn't like this idea, as it was an Order N operation and didn't feel right. I have come up with a callback system, which is better but not fantastic. I have included the code for the callback system, however I was wondering if anyone had a better way of doing this?

class IContainer
{
public:
    virtual void FinalReference(BaseObject *in_obj)=0;
};

class BaseObject 
{
    unsigned int m_ref;

public:
    IContainer *m_container;

    BaseObject() : m_ref(0),m_container(0)
    {
    }

    void AddRef()
    {
        ++m_ref;
    }
    void Release()
    {
        // if we only have one reference left and we have a container
        if( 2 == m_ref && 0 != m_container )
        {
            m_container->FinalReference(this);
        }

        if( 0 == (--m_ref) )
        {
            delete this;
        }
    }
};

class Book : public BaseObject
{
    char *m_name;
public:
    Book()
    {
        m_name = new char[30];
        sprintf_s(m_name,30,"%07d",rand());
    }
    ~Book()
    {
        cout << "Deleting book : " << m_name;
        delete [] m_name;
    }

    const char *Name()
    {
        return m_name;
    }
};

class BookList : public IContainer
{
public:
    set<BookIPtr> m_books;

    void FinalReference(BaseObject *in_obj)
    {
        set<BookIPtr>::iterator it = m_books.find(BookIPtr((Book*)in_obj));
        if( it != m_books.end() )
        {
            in_obj->m_container = 0;
            m_books.erase( it );
        }
    }
};

namespace boost
{
    inline void intrusive_ptr_add_ref(BaseObject *p)
    {
        // increment reference count of object *p
        p->AddRef();
    }
    inline void intrusive_ptr_release(BaseObject *p)
    {
        // decrement reference count, and delete object when reference count reaches 0
        p->Release();
    } 
} // namespace boost

干杯

Cheers Rich

推荐答案

我从来没有使用过的boost ::侵入智能指针,但是如果你要使用的shared_ptr智能指针,你可以使用的weak_ptr对象的缓存。

I never used boost::intrusive smart pointers, but if you would use shared_ptr smart pointers, you could use weak_ptr objects for your cache.

当系统决定释放其存储器的那些的weak_ptr指针不计数作为参考,但也可以使用,只要该对象尚未删除检索一个shared_ptr

Those weak_ptr pointers do not count as a reference when the system decides to free their memory, but can be used to retrieve a shared_ptr as long as the object has not been deleted yet.

这篇关于如何从缓存中删除智能指针时,有没有更多的参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆