1

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?

Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63

1 Answers1

1

From the Standard:

unique_lock(mutex_type& m, adopt_lock_t);

Preconditions: The calling thread holds a non-shared lock on m.

So no.

Sneftel
  • 40,271
  • 12
  • 71
  • 104