1

I have some code that returns a copy the current instance of an object.

SomeObj getInstance() {
    std::lock_guard lock(m_mutex);
    return m_obj
}

m_obj can change in another thread and its copy-constructor / assignment operator are not atomic.
When return m_obj is copying the object, is the lock still held? Or do I need to do something like:

SomeObj getInstance() {
    SomeObj ret;
    {
        std::lock_guard lock(m_mutex);
        ret = m_obj;
    }
    return ret;
}
shoosh
  • 76,898
  • 55
  • 205
  • 325

0 Answers0