std::recursive mutex

suggest change

Recursive mutex allows the same thread to recursively lock a resource - up to an unspecified limit.

There are very few real-word justifications for this. Certain complex implementations might need to call an overloaded copy of a function without releasing the lock.

std::atomic_int temp{0};
std::recursive_mutex _mutex;

//launch_deferred launches asynchronous tasks on the same thread id

auto future1 = std::async(
            std::launch::deferred,
            [&]()
            {
                std::cout << std::this_thread::get_id() << std::endl;

                std::this_thread::sleep_for(std::chrono::seconds(3));
                std::unique_lock<std::recursive_mutex> lock( _mutex);
                temp=0;
                
            });

auto future2 = std::async(
            std::launch::deferred,
            [&]()
            {
                std::cout << std::this_thread::get_id() << std::endl;
                while ( true )
                {
                    std::this_thread::sleep_for(std::chrono::milliseconds(1));
                    std::unique_lock<std::recursive_mutex> lock( _mutex, std::try_to_lock);
                    if ( temp < INT_MAX )
                         temp++;

                    cout << temp << endl;
                    
                }
            });
future1.get();
future2.get();

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Mutexes:
* std::recursive mutex

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype