Is it safe to hold a std::lock_guard in the destructor?
According to the standard in [class.dtor]/9,
After executing the body of the destructor and destroying any automatic objects allocated within the body, a destructor for class
X
calls the destructors forX
’s direct non-variant non-static data members, the destructors forX
’s non-virtual direct base classes and, ifX
is the type of the most derived class (15.6.2), its destructor calls the destructors forX
’s virtual base classes. ...
This answers your question in the affirmative.
The destructor's body is executed before any member is destructed. In this sense it's safe.
But prior to the question if it is safe the question if there is any sensible use case to hold the mutex in the destructor (as well as the constructor) must be asked.
Only one thread can construct or destruct an object. This must be assured by a mutex outside of the object, e.g. at construction in the factory or at destruction in the shared pointer.