开发者

How to deal with failure to destroy mutex in destructor

Say you have the following destructor in a mutex class wrapping u开发者_如何学Gop 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜