« Return to Thread: [thread] is this reverse_lock class a source of errors?

[thread] is this reverse_lock class a source of errors?

by Vicente Botet Escriba :: Rate this Message:

Reply to Author | View in Thread

Hi,

Consider the following situations:

 {
     unique_lock<mutex> lock(smtx);

     // ... some writing operations

     { // non locked block
         reverse_lock< unique_lock<mutex> > rlock(lock);
         // ... some code not needing the mutex to be locked
     } // locked again

     // ...
}

or

 {
     shared_lock<mutex> lock(smtx);

     // ... some reading operations

     { // non locked block
         reverse_lock< shared_lock<mutex> > rlock(lock);
         // ... some code not needing the mutex to be locked
     } // locked again

     // ...
}

Do you think this usage is souhaitable or is this source of errors?

The class reverse_lock can be defined as follows:

template <typename Locker>
class reverse_lock
{
    BOOST_CONCEPT_ASSERT((MovableLockConcept<Locker>));
public:
    reverse_lock(Locker& locker)
        : locker_(locker) /*< Store reference to locker >*/
        , tmp_locker_(locker.move()) /*< Move ownership to temporaty locker
 >*/
        , was_locked_(false)
    {
 #ifndef BOOST_SYNC_REVERSE_LOCK_DONT_CHECK_OWNERSHIP
    /*< Define BOOST_SYNC_REVERSE_LOCK_DONT_CHECK_OWNERSHIP
        if you don't want to check locker ownership >*/
        if (tmp_locker_.mutex()==0) {
            locker_=tmp_locker_.move(); /*< Rollback for coherency purposes
 >*/
            throw lock_error();
        }
#endif
        if (tmp_locker_) { /*< ensures it is unlocked >*/
            tmp_locker_.unlock();
            was_locked_=true;
        }
    }
    ~reverse_lock() {
        if (was_locked) {
            tmp_locker_.lock();
        }
        locker_=tmp_locker_.move(); /*< Move ownership to nesting locker >*/
    }
private:
    Locker& locker_;
    Locker tmp_locker_;
    bool was_locked_;
    reverse_lock();
};

Comments?
_____________________
Vicente Juan Botet Escriba


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

 « Return to Thread: [thread] is this reverse_lock class a source of errors?