In effect, if you would like to thoroughly test and debug your application and don't really care about performance (right now, at least), then set up your mutex as follows:
- Set the robust attribute on it (allowing one to catch the owner-dies-without-unlocking case): pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST)
- Set the type to error checking (allowing one to catch the self-deadlock / relock case): pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)
On the other hand, a well-designed and proven application that requires you to squeeze out performance would use the normal (default) mutex type and attributes. The preceding cases will not be caught (and will instead result in undefined behavior), ...