Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 797
Invictus Avatar asked Feb 12 '12 07:02

Invictus


1 Answers

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.

like image 155
Sergey Shamov Avatar answered Oct 13 '22 02:10

Sergey Shamov