1
\$\begingroup\$

Is this my_upgrade_mutex class a valid implementation of boost::upgrade_mutex semantics? (Ignoring the try_* and *_for/*_until part.)

class my_upgrade_mutex
{
 std::mutex xmutex;
 std::shared_mutex smutex;
public:
 void lock_shared()//u->s
 {
 smutex.lock_shared();
 }
 void unlock_shared()//s->u
 {
 smutex.unlock_shared();
 }
 void lock()//u->x
 {
 xmutex.lock();
 smutex.lock();
 }
 void unlock()//x->u
 {
 smutex.unlock();
 xmutex.unlock();
 }
 void lock_upgrade()//u->g
 {
 xmutex.lock();
 smutex.lock_shared();
 }
 void unlock_upgrade()//g->u
 {
 smutex.unlock_shared();
 xmutex.unlock;
 }
 void unlock_upgrade_and_lock()//g->x
 {
 smutex.unlock_shared();
 smutex.lock();
 }
 void unlock_and_lock_upgrade()//x->g
 {
 smutex.unlock();
 smutex.lock_shared();
 }
 void unlock_and_lock_shared()//x->s
 {
 smutex.unlock();
 smutex.lock_shared()
 xmutex.unlock();
 }
 void unlock_upgrade_and_lock_shared()//g->s
 {
 xmutex.unlock();
 }
};
asked May 31, 2020 at 15:26
\$\endgroup\$
1
  • 2
    \$\begingroup\$ I'm new here. Does anyone have any hints about the down votes, please? \$\endgroup\$ Commented May 31, 2020 at 16:36

1 Answer 1

1
\$\begingroup\$

I think it actually is valid.

Here's all of the possible states of the two mutexes.

/* u: unlocked, s: shared locked, g: upgrade locked, x: exclusive locked
 (state): of which upgrade_mutex state, the states are as this line
 (inter-state): in the middle of these transitions, the states might be as this line
 Xab means the original state is of symbol X,
 and the thread is doing the lock/unlock of a->b
 symbol xmutex smutex (state) (inter-state)
 A u u u
 B x u Aux Axu Aug Agu Agx Axg Axs Dgu Dgx Exu Exg Exs
 C u s s Cs
 D x s g Axs Bs Cux Cug Ds Dgu Dgx Exs
 E x x x
*/
  1. The 10 lock/unlock methods all behave reasonably good in all of the states
  2. Given the prior-method state is in the set, the during-/post-method state is in the set
answered Jun 1, 2020 at 0:58
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.