multithreading - C++: locking mechanism with a container of a set of pointers -


i have a

std::map<std::string, something*> mymap 

where something* can locked because can accessed different threads.

since entries in mymap can inserted, have global lock avoid concurrency during "fetch" phase.

mymaplock.lock(); something* = mymaplock[key]; mymaplock.unlock(); some.lock(); // some.unlock(); 

this structure cannot work if want delete entries in mymap, since remove still locked else (but has unlocked mymap).

do know of pattern solve problem?

you can use std::shared_ptr<something> instead of something *. something object deleted when reference count == 0. if thread has locked something object (and copy of std::shared_ptr<something> mymap) something object not deleted if other thread removes entry something object mymap.


Comments

Popular posts from this blog

css - I want to align grid in center -

Contact Form PHP Email Script -

python - SWIG function not printing output -