I wanted to use a std::mutex
in my class, and noticed that it isn't copyable. I'm at the bottom level of my library here, so it seems like a terrible idea to have this behaviour.
I used std::lock_guard
on the std::mutex
, but there doesn't seem to be a shared_lock_guard
, which would be preferable to provide write-locks-exclusively behaviour. Is this an oversight or trivial to implement myself?
With
C++14
You can use a std::shared_lock and a std::unique_lock to implement read/write locking:Note: If you have
C++17
, then you can usestd::shared_mutex
formutex_type
.It’s not part of C++ standard yet, but you can find implementation example in boost.
It requires mutex class conforming to SharedMutex concept though; std::shared_mutex is part of proposed C++17 standard and boost had one already for some time: boost::shared_mutex.