How to avoid priority inversion in a POSIX program? Pthread allows to choose the protocol PTHREAD_PRIO_INHERIT on a mutex. And how to use this feature in a standard C++ mutex?

It seems feasible to derive a custom mutex from std::mutex, while reinitializing the underlying POSIX mutex in the constructor. That’s a non-portable hackery, yet program stability should prevail:

class InheritPrioMutex : public std::mutex
{
    InheritPrioMutex()
    {
        // Destroy the underlying mutex
        ::pthread_mutex_destroy(native_handle());

        // Create mutex attribute with desired protocol
        ::pthread_mutexattr_t attr;
        ::pthread_mutexattr_init(&attr);
        ::pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
        // Initialize the underlying mutex
        ::pthread_mutex_init(native_handle(), &attr);
        // The attribute shouldn't be needed any more
        ::pthread_mutexattr_destroy(&attr);
    }
};