简体   繁体   中英

How to deal with failure to destroy mutex in destructor

Say you have the following destructor in a mutex class wrapping up pthread mutex calls:

~mutex()
{
    pthread_mutex_destroy(&m_mutex);
}

If this fails (returns non-zero) we can't throw an exception obviously. How best do we deal with this?

Write an error message and call abort(). Hard, visible failure is often preferable to continuing blithely on when the impossible appears to have happened.

I don't think there's a lot you can do other than ignore it (possibly logging a message, especially if you get EBUSY since this could indicate a serious logic error in your program).

You may take a look at boost::threads: if you are building release - return code will not be checked, and if you are build debug version - abort() will be called with error message printed, BOOST_VERIFY is user for this

In my opinion, the only sane recourse in such a case is assert(3) - something went horribly wrong, so somebody has to investigate...

I suggest a run-time assertion. If it fails, you are in posix's land of undefined behavior.

The fact that it's inside a destructor is irrelevant. The problem is that you cannot recover if destruction fails (except ignoring it). It's always the case, no matter what language you use.

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