std::recursive_mutex::try_lock

From cppreference.com

 
 
Concurrency support library
Threads
(C++11)
(C++20)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Free functions for atomic operations
Free functions for atomic flags
Memory ordering
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
 
 
bool try_lock();
(since C++11)

Tries to lock the mutex. Returns immediately. On successful lock acquisition returns true, otherwise returns false.

This function is allowed to fail spuriously and return false even if the mutex is not currently locked by any other thread.

A thread may call try_lock on a recursive mutex repeatedly. Successful calls to try_lock increment the ownership count: the mutex will only be released after the thread makes a matching number of calls to unlock.

The maximum number of levels of ownership is unspecified. A call to try_lock will return false if this number is exceeded.

Prior unlock() operation on the same mutex synchronizes-with (as defined in std::memory_order) this operation if it returns true. Note that prior lock() does not synchronize with this operation if it returns false.

Parameters

(none)

Return value

true if the lock was acquired successfully, otherwise false.

Exceptions

Throws nothing.

Example

#include <iostream>
#include <mutex>
 
int main()
{
    std::recursive_mutex test;
    if (test.try_lock()) {
        std::cout << "lock acquired\n";
        test.unlock();
    } else {
        std::cout << "lock not acquired\n";
    }
 
    test.lock();
    // non-recursive mutex would return false from try_lock now
    if (test.try_lock()) {
        std::cout << "lock acquired\n";
        test.unlock(); 
    } else {
        std::cout << "lock not acquired\n";
    }
    test.unlock();
}

Output:

lock acquired
lock acquired

See also

locks the mutex, blocks if the mutex is not available
(public member function)
unlocks the mutex
(public member function)