vendredi 23 septembre 2016

thread safty of find() from a STL container of std::unique_ptr

Example code.

class Obj
{
    public:
    void doSome(void)
    {
        std::cout << "Hello World!" << std::endl;
    }
};

std::unordered_map<int, std::unique_ptr<Obj>> map;

// insert
map[123] = std::move( std::unique_ptr<Obj>(new Obj) );

// find
auto search = map.find(123);  // <=== (Q)
if (search != map.end())      
{
  search->second->doSome();
}

(Q)

How about the thread safty if there are multiple threads running //find section?

will map.find(123) always find the obj in every thread? as long as the search->second not assigned to someone else?

Aucun commentaire:

Enregistrer un commentaire