I have two use cases.
A. I want to synchronise access to a queue for two threads.
B. I want to synchronise access to a queue for two threads and use a condition variable because one of the threads will wait on content to be stored into the queue by the other thread.
For use case A I see code example using std::lock_guard<>
. For use case B I see code example using std::unique_lock<>
.
What is the difference between the two and which one should I use in which use case?
This question is related to
c++
multithreading
c++11
mutual-exclusion
stdmutex
One missing difference is:
std::unique_lock
can be moved but std::lock_guard
can't be moved.
Note: Both cant be copied.
There are certain common things between lock_guard
and unique_lock
and certain differences.
But in the context of the question asked, the compiler does not allow using a lock_guard
in combination with a condition variable, because when a thread calls wait on a condition variable, the mutex gets unlocked automatically and when other thread/threads notify and the current thread is invoked (comes out of wait), the lock is re-acquired.
This phenomenon is against the principle of lock_guard
. lock_guard
can be constructed only once and destructed only once.
Hence lock_guard
cannot be used in combination with a condition variable, but a unique_lock
can be (because unique_lock
can be locked and unlocked several times).
Use lock_guard
unless you need to be able to manually unlock
the mutex in between without destroying the lock
.
In particular, condition_variable
unlocks its mutex when going to sleep upon calls to wait
. That is why a lock_guard
is not sufficient here.
If you're already on C++17 or later, consider using scoped_lock
as a slightly improved version of lock_guard
, with the same essential capabilities.
As has been mentioned by others, std::unique_lock tracks the locked status of the mutex, so you can defer locking until after construction of the lock, and unlock before destruction of the lock. std::lock_guard does not permit this.
There seems no reason why the std::condition_variable wait functions should not take a lock_guard as well as a unique_lock, because whenever a wait ends (for whatever reason) the mutex is automatically reacquired so that would not cause any semantic violation. However according to the standard, to use a std::lock_guard with a condition variable you have to use a std::condition_variable_any instead of std::condition_variable.
Edit: deleted "Using the pthreads interface std::condition_variable and std::condition_variable_any should be identical". On looking at gcc's implementation:
lock_guard
and unique_lock
are pretty much the same thing; lock_guard
is a restricted version with a limited interface.
A lock_guard
always holds a lock from its construction to its destruction. A unique_lock
can be created without immediately locking, can unlock at any point in its existence, and can transfer ownership of the lock from one instance to another.
So you always use lock_guard
, unless you need the capabilities of unique_lock
. A condition_variable
needs a unique_lock
.
They are not really same mutexes, lock_guard<muType>
has nearly the same as std::mutex
, with a difference that it's lifetime ends at the end of the scope (D-tor called) so a clear definition about these two mutexes :
lock_guard<muType>
has a mechanism for owning a mutex for the duration of a scoped block.
And
unique_lock<muType>
is a wrapper allowing deferred locking, time-constrained attempts at locking, recursive locking, transfer of lock ownership, and use with condition variables.
Here is an example implemetation :
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <chrono>
using namespace std::chrono;
class Product{
public:
Product(int data):mdata(data){
}
virtual~Product(){
}
bool isReady(){
return flag;
}
void showData(){
std::cout<<mdata<<std::endl;
}
void read(){
std::this_thread::sleep_for(milliseconds(2000));
std::lock_guard<std::mutex> guard(mmutex);
flag = true;
std::cout<<"Data is ready"<<std::endl;
cvar.notify_one();
}
void task(){
std::unique_lock<std::mutex> lock(mmutex);
cvar.wait(lock, [&, this]() mutable throw() -> bool{ return this->isReady(); });
mdata+=1;
}
protected:
std::condition_variable cvar;
std::mutex mmutex;
int mdata;
bool flag = false;
};
int main(){
int a = 0;
Product product(a);
std::thread reading(product.read, &product);
std::thread setting(product.task, &product);
reading.join();
setting.join();
product.showData();
return 0;
}
In this example, i used the unique_lock<muType>
with condition variable
Source: Stackoverflow.com