Guide
basic
ownership
- shared ownership
- exclusive ownership
- upgrade ownership—>exclusive ownership
mutex
- boost::mutex enable exclusive access to shared data.
- boost::shared_mutex enable shared access to shared data.
lock
- boost::shared_lock
- boost::unique_lock
- boost::upgrade_lock
- boost::upgrade_to_unique_lock
Tips from difference-between-boostunique-lock-and-boostupgrade-lock
The difference between
upgrade_lockandunique_lockis simple. An instance ofunique_lockis acquiring a full exclusive ownership of ashared_mutex. This means that no one else can get any type of ownership while theunique_lockis alive.
Unlike the
unique_lockan instance ofupgrade_lockis acquiring an upgrade ownership that exclusive only amongst threads trying to get the same upgrade ownership. All other threads that try to get a shared ownership could acquire it with no conflict until theupgrade_lockis upgraded to unique (with an instance ofupgrade_to_unique_lock).
The
upgrade_lockis useful when some of threads can be readers only and will not try to promote itself to writers. Otherwise (all readers may try to become writers at some point)upgrade_lockwill operate asunique_lock.
conclusions
thread-A get
shared_lock,other threads cannot getunique_lock,upgrade_lock,but can getshared_lock.(multiple-reader)thread-A get
upgrade_lock,other threads cannot getunique_lock,upgrade_lock,but can getshared_lock.upgrade_lockcan upgrade toupgrade_to_unique_lock,it’s same as thread-A getunique_lock.thread-A get
unique_lock,other threads cannot getunique_lock,upgrade_lock,shared_lock. (single-writer)
code example
1 |
|
Reference
- synchronization
-example-for-boost-shared-mutex-multiple-reads-one-write - difference-between-boostunique-lock-and-boostupgrade-lock
History
- 20180523 created.