tighten up locking

This commit is contained in:
Hieu Hoang 2017-01-25 16:25:33 +00:00
parent 3aed45cd47
commit 138a414ddd
2 changed files with 13 additions and 9 deletions

View File

@ -274,17 +274,21 @@ DeviceInfo God::GetNextDevice() const
Search &God::GetSearch() const
{
Search *obj;
{
boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
obj = m_search.get();
boost::shared_lock<boost::shared_mutex> read_lock(accessLock_);
obj = search_.get();
if (obj) {
// found exiting obj
return *obj;
}
}
if (obj == NULL) {
boost::unique_lock<boost::shared_mutex> lock(m_accessLock);
// create new obj
boost::unique_lock<boost::shared_mutex> lock(accessLock_);
obj = new Search(*this);
search_.reset(obj);
obj = new Search(*this);
m_search.reset(obj);
}
assert(obj);
return *obj;
}

View File

@ -100,6 +100,6 @@ class God {
mutable size_t threadIncr_;
mutable boost::thread_specific_ptr<Search> m_search;
mutable boost::shared_mutex m_accessLock;
mutable boost::thread_specific_ptr<Search> search_;
mutable boost::shared_mutex accessLock_;
};