Search code examples
c++multithreadingunique-lock

Does this unique_lock in dtor serve any purpose?


Ran across this destructor in a codebase I am debugging.

ManagerImpl::~ManagerImpl() {
    // don't go away if some thread is still hitting us
    boost::unique_lock<boost::mutex> l(m_mutex);
}

Does it actually serve any useful purpose in a multi-threaded program? It looks like kludge.

I assume the idea is to defer destruction if another thread is calling a function that locks the mutex, but is it even effectively at doing that? ElectricFence segfaults would have me believe otherwise.


Solution

  • It is probably trying to postpone destruction until another thread unlocks the mutex and leaves another member function.

    However, this wouldn't prevent another thread calling that function again after the lock in the destructor has been released.

    There must be more interaction between threads (which you don't show) to make this code make sense. Still, thought, this doesn't seem to be robust code.