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.
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
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

Anyway I am posting the code again for brevity
#include <iostream>
#include <boost/thread.hpp>
class boostThreadLocksTest
                boost::shared_mutex myMutex;
                boost::upgrade_lock<boost::shared_mutex> myLock;
                static int firstFunction(boostThreadLocksTest
                static int secondFunction(boostThreadLocksTest
int boostThreadLocksTest::firstFunction(boostThreadLocksTest
        std::cout<<"Before Locking "<<boost::this_thread::get_id()<<"
        std::cout<<"After Locking "<<boost::this_thread::get_id()<<"
        std::cout<<"Returned From Call "<<boost::this_thread::get_id()<<"
        std::cout<<"After Unlocking "<<boost::this_thread::get_id()<<"
int boostThreadLocksTest::secondFunction(boostThreadLocksTest
        std::cout<<"Before Exclusive Locking
"<<boost::this_thread::get_id()<<" "<<__PRETTY_FUNCTION__<<std::endl;
        std::cout<<"After Exclusive Locking
"<<boost::this_thread::get_id()<<" "<<__PRETTY_FUNCTION__<<std::endl;
int main()
        boostThreadLocksTest myObject;
        boost::thread_group myThreadGroup;


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, david.abrahams at, gregod at, cpdaniel at, john at