2
votes

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?

6

6 Answers

2
votes

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

1
votes

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).

1
votes

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

0
votes

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

0
votes

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

0
votes

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.