#include <string>
#include <mutex>
#include <iostream>

using namespace std;
namespace
{
    //std::mutex m;
    std::recursive_mutex m; // 可递归
    class Test
    {
    public:
        void test1()
        {
            std::lock_guard<std::recursive_mutex> lock(m);
        }
        void test2()
        {
            std::lock_guard<std::recursive_mutex> lock(m);
            test1();
            cout << "test2()" << endl;
        }
    };
}

#if 1

int main()
{
    Test t;
    t.test2();

    return 0;
}

#endif

输出:

test2()

递归上锁用recursive_mutex,否则会崩。