Why is std::lock_guard
not movable, it would make code so much nicer:
auto locked = lock_guard(mutex);
instead of
std::lock_guard<std::mutex> locked(mutex);
Is there something wrong with creating your own version, like:
template <typename T> class lock_guard_
{
T* Mutex_;
lock_guard_(const lock_guard_&) = delete;
lock_guard_& operator=(const lock_guard_&) = delete;
public:
lock_guard_(T& mutex) : Mutex_(&mutex)
{
Mutex_->lock();
}
~lock_guard_()
{
if(Mutex_!=nullptr)
Mutex_->unlock();
}
lock_guard_(lock_guard_&& guard)
{
Mutex_ = guard.Mutex_;
guard.Mutex_ = nullptr;
}
};
template <typename T> lock_guard_<T> lock_guard(T& mutex)
{
return lock_guard_<T>(mutex);
}
?
Any fundamental reason it would be a bad idea to make it movable?
unique_lock
. It might just be to make the interface as simple as possible. – Intenerateunique_lock
(though I would argue that it is mostly redundant anyway). Therefore iflock_guard
is desired as a seperate class it can't really be movable. – Bandsman