vendredi 2 août 2019

Destructing unique_ptr - swap vs std::move

I have a class with unique_ptr member (worker_) which may be used from different threads. worker_ destruction may take some time so a method which destorys that member looks like this:

void uninitialize()
{
    std::unique_ptr<Worker> worker;
    {
        std::lock_guard<std::mutex> guard(mtx_);
        worker = std::move(worker_);
    }
}

I have also seen similar implementation for such method:

void uninitialize()
{
    std::unique_ptr<Worker> worker;
    {
        std::lock_guard<std::mutex> guard(mtx_);
        worker.swap(worker_);
    }
    worker.reset();
}

Is it necessary to call reset on the worker ? What are differences between two versions of uninitialize method ?

Aucun commentaire:

Enregistrer un commentaire