I have a situation where an std::mutex needs to be locked most of the time, except when inside a specific scope. I was thinking there should be an opposite of std::unique_lock that unlock()s on construction and the lock()s on destruction.
Should be something as simple as
template<typename M>
class unique_unlock
{
public:
unique_unlock(M& m) : m_(m) {
m_.unlock();
}
~unique_unlock() {
m_.lock();
}
private:
M& m_;
};
Is there any issue with this approach?