|
Boost : |
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-05-09 06:15:27
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk