multithreading - Lock/CriticalSection vs volatile in c++ for visibility -
i have map can updated multiple thread. , critical section guards read/writes map.
ccriticalsection mapcs; map<string, string> mfilenametoinstance;
i use single lock everytime access map.
csinglelock lock(&mapcs); lock.lock(); // perform whatever reads/write map. mfilenametoinstance.insert("a", "b"); lock.unlock();
my question is,
- is sufficient guarantee visibility of mfilenametoinstance other threads or need volatile ?
if access map (both reading , writing) don't been flag volatile lock memory barrier changes visible threads.
however, flagging map volatile
won't work. according the msdn documentation volatile keyword has no effect exceeds maximum size can copied on current architecture using 1 instruction. map
class undoubtably fall under condition.
also, don't use locking mechanism requires call unlock function. csinglelock
should unlocking in destructor using raii pattern.
Comments
Post a Comment