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?
CodePudding user response:
From the Standard:
unique_lock(mutex_type& m, adopt_lock_t);
Preconditions: The calling thread holds a non-shared lock on m.
So no.