public member function
<mutex>

std::recursive_mutex::try_lock

bool try_lock() noexcept;
Lock mutex if not locked by other threads
Attempts to lock the recursive_mutex, without blocking

  • If the recursive_mutex isn't currently locked by any thread, the calling thread locks it (from this point, and until its member unlock is called, the thread owns the recursive_mutex).
  • If the recursive_mutex is currently locked by another thread, the function fails and returns false, without blocking (the calling thread continues its execution).
  • If the recursive_mutex is currently locked by the same thread calling this function, the thread acquires a new level of ownership over the recursive_mutex. Unlocking the recursive_mutex completely will require an additional call to member unlock.

This function may fail spuriously when no other thread has a lock on the recursive_mutex, but repeated calls in these circumstances shall succeed at some point.

All lock and unlock operations on the recursive_mutex follow a single total order, with all visible effects synchronized between the lock operations and previous unlock operations on the same object.

参数



返回值

true if the function succeeds in locking the recursive_mutex for the thread.
否则返回 false

数据竞争

The recursive_mutex object is accessed/modified as an atomic operation (causes no data races).

异常安全

无异常保证: 绝不抛出异常。

另见