upgrade_lock将可将读锁(shared_lock)升级为upgrade_lock,与shared_lock不互斥,与别的upgrade_lock和unique_lock互斥。也就是说线程A获得mutex的upgrade_lock后,线程B、C等还可以获得mutex的share_mutex,反之亦然。
upgrade_to_unique_lock可将upgrade_lock升级为独占锁,不允许其它线程读或写
举例
Read Lock
I need lock positionMutex whenever I’m going to read from memory that this mutex should cover. I do this by using boost::shared_lock() , which allows multiple reads without blocking as long as the mutex hasn’t been upgraded to a unique lock.
boost::shared_lock lock(this->positionMutex);
Write Lock
Whenever I write to my position properties, I need to upgrade the mutex to unique access so that other threads don’t read while the new value is being written. This is done by using boost::upgrade_lock and boost::upgrade_to_unique_lock as shown below .
boost::upgrade_lock lock(this->positionMutex);
boost::upgrade_to_unique_lock unique_lock(lock);
https://codersdesiderata.com/2017/04/26/out-of-phase-race-conditions-and-shared-mutexes/