I have a situation where I have 3 threads involved. Let's call them A, B, and C. Thread A and B share static data within the same class. Thread C also could potentially be using the same static data. So I created two mutexes. One mutex covers thread A and B sharing data with each other. Another mutex takes care of thread A and C sharing data. One mutex is a boost scoped_lock and another mutex is a lock_guard. Am I doing the right thing here? Basically, some of the methods end up looking like this:
bool CMBTmlBroker::GetTheDatasetListFromTheMB(const std::string &strDatasetTypeShortDisplayName /* use GetTheDatasetTypeShortDisplayName() */,
const std::string &strDatasetType /* can be blank */,
const std::string &strProduct /* can be blank */)
{
boost::recursive_mutex::scoped_lock scoped_lock(GetLock());
boost::lock_guard<boost::mutex> DBDatasetKey2DBDatasetInfoMutex(m_DBDatasetKey2DBDatasetInfoMutex);
...
I want a solution with performance being the biggest concern.
mutexassures mutual exclusion between any number of threads. it guarantees that only one thread will be active in the area betweenlock-----unlock. Now, you do not need to worry which one of how many is active because no matter what there will always be one thread accessing the critical code section. - Ramadheer Singh