简体   繁体   中英

Difference between boost::unique_lock and boost::upgrade_lock?

I am new to threading concept in C++ . I Just wanted to know few things: How is a boost::unique_lock different from boost::upgrade_lock ?

How actually an exclusive ownership differ from upgrade ownership.

Maybe one can say exclusive ownership is thread safe but not upgrade ownership, in that case i would like to know how can an upgrade ownership can be harmful if it can be? I want to know what is that upgrade_lock allows or not allows that unique_lock does except exclusive lock thing. Not providing exclusive lock by upgrade_lock makes it similar to shared_lock or what and if so then how is it different from shared_lock ?

@Xeo yes upgrade_lock can be upgraded to uniue_lock by upgrade_yo_unique_lock . so why do we need shared_lock is that because shared_lock cant be upgrade to unique_lock ? Is that the only thing ?

Yes.

shared_mutex exists only to supply the functionality of traditional read/write mutexes. Some clients will be understandably leery of a mutex changing lock-modes from one to another, and yet still need the traditional functionality of a rw/mutex. This is the role of shared_mutex .

Other clients will look at shared_mutex and say: I want more. I want to sometimes atomically upgrade my ownership from shared to exclusive. And this is the role of upgrade_mutex . upgrade_mutex is not better than shared_mutex , nor vice-versa. It is simply another tool in the toolbox.

The difference between upgrade_lock and unique_lock is simple. An instance of unique_lock is acquiring a full exclusive ownership of a shared_mutex . This means that no one else can get any type of ownership while the unique_lock is alive.

Unlike the unique_lock an instance of upgrade_lock is 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 the upgrade_lock is upgraded to unique (with an instance of upgrade_to_unique_lock ).

The upgrade_lock is 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_lock will operate as unique_lock .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM