The C++ standard library lacks an upgrade_lock
method, similar to what boost thread provides. But looking at the adopt_lock
facilities, one might be tempted to do the following:
// Declare a shared mutex
std::shared_mutex mtx;
// Create a reader lock
std::shared_lock rlock(mtx);
// Other code and program logic ...
// Adopt the lock into a unique lock
std::unique_lock wlock(mtx, std::adopt_lock);
// Disassociate the reader lock to avoid doubly unlocking
rlock.unlock();
Is this valid / safe ? If not, is there a common practice on solving this?