Strategies for lock classes std try to lock std adopt lock std defer lock
suggest changeWhen creating a std::unique_lock, there are three different locking strategies to choose from: std::try_to_lock, std::defer_lock and std::adopt_lock
std::try_to_lockallows for trying a lock without blocking. Neither loop setstempto-1or otherwise stops, so this block does not finish:
{
std::atomic_int temp {0};
std::mutex _mutex;
std::thread t( [&](){
while( temp!= -1){
std::this_thread::sleep_for(std::chrono::seconds(5));
std::unique_lock<std::mutex> lock( _mutex, std::try_to_lock);
if(lock.owns_lock()){
//do something
temp=0;
}
}
});
while ( true )
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::unique_lock<std::mutex> lock( _mutex, std::try_to_lock);
if(lock.owns_lock()){
if (temp < INT_MAX){
++temp;
}
std::cout << temp << std::endl;
}
}
}
std::defer_lockallows for creating a lock structure without acquiring the lock. When locking more than one mutex, there is a window of opportunity for a deadlock if two callers acquire the same mutexes in opposite orders. This snippet has no second thread, so the twolock()calls by themselves do not deadlock:
{
std::unique_lock<std::mutex> lock1(_mutex1, std::defer_lock);
std::unique_lock<std::mutex> lock2(_mutex2, std::defer_lock);
lock1.lock();
lock2.lock(); // deadlocks if another thread locks these mutexes in the opposite order
std::cout << "Locked!" << std::endl;
//...
}
With the following code, whatever happens in the function, the locks are acquired and released in appropriate order:
{
std::unique_lock<std::mutex> lock1(_mutex1, std::defer_lock);
std::unique_lock<std::mutex> lock2(_mutex2, std::defer_lock);
std::lock(lock1,lock2); // no deadlock possible
std::cout << "Locked!" << std::endl;
//...
}
std::adopt_lockassumes the calling thread already owns the mutex and does not lock. If this thread does not own it, the behavior is undefined.
{
std::unique_lock<std::mutex> lock1(_mutex1, std::adopt_lock);
std::unique_lock<std::mutex> lock2(_mutex2, std::adopt_lock);
std::cout << "Locked!" << std::endl;
//...
}
Something to keep in mind is that std::adopt_lock is not a substitute for recursive mutex usage. When the lock goes out of scope the mutex is released.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents