I have the following lines of code where I used C++ Boost thread:
void threadFunc()
{
boost::mutex::scoped_lock lock(m_Mutex);
//some code here...
condition.notify_one();
}
So should I call unlock() function before the last line, like the following? What is the difference if I don’t call unlock()?
void threadFunc()
{
boost::mutex::scoped_lock lock(m_Mutex);
//some code here...
lock.unlock();
condition.notify_one();
}
No — the point of the
scoped_lockclass is that the lock is tied to the scope — i.e., when thescoped_lockobject goes out of scope, the lock is automatically released. This assures (for example) that if any of the intervening code throws an exception, the lock will still be released.