std::try_lock

From Cppreference

Jump to: navigation, search
Defined in header <mutex>

template< class Lockable1, class Lockable2, class LockableN... >
int try_lock( Lockable1& lock1, Lockable2& lock2, LockableN lockn... );
(C++11 feature)

Tries to lock each of the given lockable objects lock1, lock2, ..., lockn by calling try_lock() in order beginning at the first. If a call to try_lock() fails, unlock() is called for any locked objects and a 0-based index of the object that failed to lock, is returned.

If a call to try_lock() results in an exception, unlock() is called for any locked objects before rethrowing.

Contents

[edit] Parameters

lock1, lock2, ... , lockn - the lockable objects to lock

[edit] Return value

-1 on success or 0-based index value of the object that failed to lock.

[edit] Example

[edit] See also

lock (C++11)
locks specified mutexes/locks, blocks if at least one is unavailable
(function template)