Graeme
Graeme

Reputation: 4602

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?

Upvotes: 2

Views: 1159

Answers (6)

tvn
tvn

Reputation: 634

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

Upvotes: 1

Alan Stokes
Alan Stokes

Reputation: 18972

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

Upvotes: 2

Yakov Galka
Yakov Galka

Reputation: 72539

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.

Upvotes: 1

John
John

Reputation: 2326

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

Upvotes: 0

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

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

Upvotes: 0

NPE
NPE

Reputation: 500873

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

Upvotes: 1

Related Questions