I have this code:
std::map<std::pair<int, int>, std::string> data;
...
ContextMutex context_mutex( _mutex );
auto pair_iterator = data.find( std::make_pair(some, thing));
if(pair_iterator == data.end())
{
context_mutex.unlock();
// do some logging without using pair_iterator
}
context_mutex.unlock();
On this code, I only release the mutex for my STL container after checking if pair_iterator == data.end()
. I wonder if I can release the mutex before checking pair_iterator == data.end()
because I do not use the pair_iterator
or any of its contents as I just print a log if the object if found.
On this case, can I release the mutex before checking the condition pair_iterator == data.end()
(as the following example) or the access to the data.end()
needs to be protected by a mutex too (as done in the first example)?
std::map<std::pair<int, int>, std::string> data;
...
ContextMutex context_mutex( _mutex );
auto pair_iterator = data.find( std::make_pair(some, thing));
context_mutex.unlock();
if(pair_iterator == data.end())
{
// do some logging without using pair_iterator
}
Related: