vendredi 26 juin 2015

Testing if object is not deleted

My object creates an thread and that thread modifies object-creator during his life cycle. The problem is, that thread should not invoke objects methods when it is destroyed. I have found some sollution for that problem and I would like to know if it is best one.

class A
{
    shared_ptr<int> guard = make_shared<int>(0);
public:
    weak_ptr<int> getGuard() { return guard; }
    void method() {}
    A()
    {
        thread([this]
        {
            const auto &guard = getGuard();
            while(!guard.expired()) 
                method();
        });
    }
};

Aucun commentaire:

Enregistrer un commentaire