简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM