Q:

How to use unique_lock in a class that calls other functions that use unique_lock?

I have a class that I need to make thread-safe. I'm trying to do this by putting a unique lock at the top of every function in the class. The problem is that as soon as one function calls another function (in this class) the mutexes seem to lock each other, despite being in different functions. How can I stop this from happening?
An example is a class with get() and set() functions that both use a unique_lock at the start of each function. But in set() you want to call get() at some point, but without set()'s mutex locking get()'s mutex. However the mutex in get() should still work if called directly.

A:

Making a class "thead safe" by adding a mutex to all operations is code smell.  Doing so with recursive mutex is worse, because it implies a lack of control and understanding about what was locked and what operations lock.
While it often permits some limited multithreaded access, but leads very often to deadlocks, contention and performance hell down the lane.
Lock based concurrency does not safely compose except in limited cases.  You can take two correct lock-based datastructures/algorithms, connect them, and end up with incorrect/unsafe code.
Consider leaving your type single threaded, implementing const methods that can be mutually called without synchronization, then using mixtures of immutable instances and externally synchronized ones.
template<class T>
struct mutex_guarded {
  template<class F>
  auto read( F&& f ) const {
    return access( std::forward<F>(f), *this );
  }
  template<class F>
  auto write( F&& f ) {
    return access( std::forward<F>(f), *this );
  }
  mutex_guarded()=default;
  template<class T0, class...Ts,
    std::enable_if_t<!std::is_same<mutex_guarded, std::decay_t<T0>>, bool> =true
  >
  mutex_guarded(T0&&t0, Ts&&ts):
    t(std::forward<T0>(t0),std::forward<Ts>(ts)...)
  {}
private:
  template<class F, class Self>
  friend auto access(F&& f, Self& self ){
    auto l = self.lock();
    return std::forward<F>(f)( self.t );
  }
  mutable std::mutex m;
  T t;
  auto lock() const { return std::unique_lock<std::mutex>(m); }
};

and similar for shared mutex (it has two lock overloads).  access can be made public and vararg woth a bit of work (to handle things like assignment).
Now calling your own methods is no problem.  External use looks like:
std::mutex_guarded<std::ostream&> safe_cout(std::cout);
safe_cout.write([&](auto& cout){ cout<<"hello "<<"world\n"; });

you can also write async wrappers (that do tasks in a thread pool and return futures) and the like.

A:

The std::recursive_mutex is what you want. It can be locked more than one time in one thread.

