function template
<mutex>
std::try_lock
template <class Mutex1, class Mutex2, class... Mutexes>
int try_lock (Mutex1& a, Mutex2& b, Mutexes&... cde);
Try to lock multiple mutexes
Attempts to lock all the objects passed as arguments using their try_lock member functions (non-blocking).
The function calls the try_lock member function for each argument (first a, then b, and eventually the others in cde, in the same order), until either all calls are successful, or as soon as one of the calls fails (either by returning false
or throwing an exception).
If the function ends because a call fails, unlock is called on all objects for which the call to try_lock was successful, and the function returns the argument order number of the object whose lock failed. No further calls are performed for the remaining objects in the argument list.
Parameters
- a, b, cde
- Objects to try-lock.
Mutex1, Mutex2 and Mutexes shall be lockable types.
Return value
In case the function is successful locking all objects, it returns -1
.
Otherwise, the function returns the index of the object which failed to be locked (0
for a, 1
for b,...).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
// std::lock example
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::try_lock
std::mutex foo,bar;
void task_a () {
foo.lock();
std::cout << "task a\n";
bar.lock();
// ...
foo.unlock();
bar.unlock();
}
void task_b () {
int x = try_lock(bar,foo);
if (x==-1) {
std::cout << "task b\n";
// ...
bar.unlock();
foo.unlock();
}
else {
std::cout << "[task b failed: mutex " << (x?"foo":"bar") << " locked]\n";
}
}
int main ()
{
std::thread th1 (task_a);
std::thread th2 (task_b);
th1.join();
th2.join();
return 0;
}
| |
Possible output:
task a
[task b failed: mutex foo locked]
|
Either mutex may appear locked to task_b, or both tasks may succeed in either order (text can appear intermingled on failure).
Data races
The arguments are modified.
Exception safety
Provides the same level of guarantees as the operation performed on the arguments.
See also
- lock
- Lock multiple mutexes (function template
)