Boost logo

Threads-Devel :

Subject: Re: [Threads-devel] Request Help On boost::lock_error
From: Ashutosh Warikoo (awarikoo_at_[hidden])
Date: 2010-09-15 00:20:52


Hello Vicente

>Why do you use a shared mutex if you lock it exclusively in firstFunction
and secondFunction?
The myLock is defined as an upgrade_lock in the class definition.
Executing a lock() operation on a upgrade_lock actually takes upgrade
ownership & not exclusive ownership. So I am not locking it exclusively in
the first function only locking it "upgradely" (sic). Here is the
definition of the lock() function.
locks.hpp
00846 void lock()
00847 {
00848 if(owns_lock())
00849 {
00850 boost::throw_exception(boost::lock_error());
00851 }
00852 m->lock_upgrade();
00853 is_locked=true;
00854 }

>upgrade_to_unique_lock has a sens only if the mutext has been
lock_shared.
upgrade_to_unique_lock has no relation with shared_lock. However it does
use the shared_mutex as core entity. If a lock has to be upgraded it has
to be of type upgrade_lock & not of shared_lock which is what I need.
Before entering a transaction I have no idea if I would need to update the
variable. In addition upgrade_to_unique_lock takes input a upgrade_lock &
not a shared_lock.

>Shouldn't you user lock_shared()/unlock_shared() in firstFunction?
If you notice, the lock is a upgrade_lock & not a shared_lock. So I don't
have the functions lock_shared()/unlock_shared(); as its not a
shared_lock.

Anyway I am posting the code again for brevity
#include <iostream>
#include <boost/thread.hpp>
class boostThreadLocksTest
{
        public:
                boost::shared_mutex myMutex;
                boost::upgrade_lock<boost::shared_mutex> myLock;
                static int firstFunction(boostThreadLocksTest
*pBoostThreadLocksTest);
                static int secondFunction(boostThreadLocksTest
*pBoostThreadLocksTest);
 boostThreadLocksTest():myMutex(),myLock(myMutex,boost::defer_lock_t()){};
};
int boostThreadLocksTest::firstFunction(boostThreadLocksTest
*pBoostThreadLocksTest)
{
        std::cout<<"Before Locking "<<boost::this_thread::get_id()<<"
"<<__PRETTY_FUNCTION__<<std::endl;
        pBoostThreadLocksTest->myLock.lock();
        std::cout<<"After Locking "<<boost::this_thread::get_id()<<"
"<<__PRETTY_FUNCTION__<<std::endl;
        pBoostThreadLocksTest->secondFunction(pBoostThreadLocksTest);
        std::cout<<"Returned From Call "<<boost::this_thread::get_id()<<"
"<<__PRETTY_FUNCTION__<<std::endl;
        pBoostThreadLocksTest->myLock.unlock();
        std::cout<<"After Unlocking "<<boost::this_thread::get_id()<<"
"<<__PRETTY_FUNCTION__<<std::endl;
        return(0);
}
int boostThreadLocksTest::secondFunction(boostThreadLocksTest
*pBoostThreadLocksTest)
{
        std::cout<<"Before Exclusive Locking
"<<boost::this_thread::get_id()<<" "<<__PRETTY_FUNCTION__<<std::endl;
        boost::upgrade_to_unique_lock<boost::shared_mutex>
localUniqueLock(pBoostThreadLocksTest->myLock);
        std::cout<<"After Exclusive Locking
"<<boost::this_thread::get_id()<<" "<<__PRETTY_FUNCTION__<<std::endl;
        return(0);
}
int main()
{
        boostThreadLocksTest myObject;
        boost::thread_group myThreadGroup;
 
myThreadGroup.create_thread(boost::bind(boostThreadLocksTest::firstFunction,&myObject));
 
myThreadGroup.create_thread(boost::bind(boostThreadLocksTest::firstFunction,&myObject));
 
myThreadGroup.create_thread(boost::bind(boostThreadLocksTest::firstFunction,&myObject));
        myThreadGroup.join_all();
}

Thanks
bornlibra23

Disclaimer note on content of this message including enclosure(s)and
 attachments(s): The contents of this e-mail are the privileged and
 confidential material of National Stock Exchange of India Limited
 (NSE). The information is solely intended for the individual/entity
 it is addressed to. If you are not the intended recipient of this
 message, please be aware that you are not authorized in any which
 way whatsoever to read, forward, print, retain, copy or disseminate
 this message or any part of it. If you have received this e-mail in
 error, we would request you to please notify the sender immediately
 by return e-mail and delete it from your computer. This e-mail
 message including attachment(s), if any, is believed to be free of
 any virus and NSE is not responsible for any loss or damage arising
 in any way from its use.



Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk