lundi 25 septembre 2017

C++ return function lock_guard

I have the resource "resource" that wrapped in shared_ptr, and I want to access it from other threads. When I do this:

// foo.h
class Foo{
public:
    std::shared_ptr<Setup> GetSomeThing();
    std::shared_ptr<Setup> resource;
}
//Foo.cpp
std::shared_ptr<Setup> Foo::GetSomeThing()
{
    std::lock_guard<std::mutex> lock (mutex); // TODO: 
    return resource;
}

is it all ok? When will be created a return object and when will be destroyed the lock? Does it exist something about it in the documentation? Thank you!

Aucun commentaire:

Enregistrer un commentaire