Re: [Boost-bugs] [Boost C++ Libraries] #4648: boost::lock_error Thrown While Using Upgrade Locks

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #4648: boost::lock_error Thrown While Using Upgrade Locks
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-12-04 18:10:27


#4648: boost::lock_error Thrown While Using Upgrade Locks
-------------------------------+--------------------------------------------
  Reporter: bornlibra23 | Owner: anthonyw
      Type: Support Requests | Status: closed
 Milestone: | Component: thread
   Version: Boost 1.43.0 | Severity: Problem
Resolution: invalid | Keywords: Boost.Thread boost::lock_error Synchronization
-------------------------------+--------------------------------------------
Changes (by viboes):

  * status: new => closed
  * type: Bugs => Support Requests
  * resolution: => invalid
  * milestone: To Be Determined =>

Comment:

 Well, after looking at the code I think I understand better the text on
 the documentation and you were right. The upgrade_lock in the class is
 used by all the threads, but there is is_locked variable that is not
 protected. Is thread_safe to share a shared_mutex between several threads,
 but not a _lock.

 I think that you should not have a upgrade_lock member, but that these
 should be on the stack.

 Here it is how it could work

 HTH. I close the ticket. Reopen it if you need more help.

 {{{
 #include <iostream>
 #include <boost/thread.hpp>
 #include <boost/current_function.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,
       boost::upgrade_lock<boost::shared_mutex>& upgr);
   boostThreadLocksTest()
     :myMutex()
      //, myLock(myMutex,boost::defer_lock_t())
   {};
 };

 int boostThreadLocksTest::firstFunction(boostThreadLocksTest
 *pBoostThreadLocksTest)
 {
   std::cout<<"Entering "<<boost::this_thread::get_id()<<"
 "<<"firstFunction"<<std::endl;
   boost::upgrade_lock<boost::shared_mutex>
 myLock(pBoostThreadLocksTest->myMutex);
   pBoostThreadLocksTest->secondFunction(pBoostThreadLocksTest, myLock);
   std::cout<<"Returned From Call "<<boost::this_thread::get_id()<<"
 "<<"firstFunction"<<std::endl;
   std::cout<<"Returning from "<<boost::this_thread::get_id()<<"
 "<<"firstFunction"<<std::endl;
   return(0);
 }
 int boostThreadLocksTest::secondFunction(boostThreadLocksTest
 *pBoostThreadLocksTest, boost::upgrade_lock<boost::shared_mutex>& upgr) {
   std::cout<<"Before Exclusive Locking "<<boost::this_thread::get_id()<<"
 "<<"secondFunction"<<std::endl;
   boost::upgrade_to_unique_lock<boost::shared_mutex>
 localUniqueLock(upgr);
   std::cout<<"After Exclusive Locking "<<boost::this_thread::get_id()<<"
 "<<"secondFunction"<<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();
     return 0;
 }
 }}}

 The output is

 {{{
 Entering Entering Entering 0x1001001600x1001006000x1001003b0
 firstFunctionfirstFunctionfirstFunction


 Before Exclusive Locking 0x100100160 secondFunction
 After Exclusive Locking 0x100100160 secondFunction
 Returned From Call 0x100100160 firstFunction
 Returning from 0x100100160 firstFunction
 Before Exclusive Locking 0x100100600 secondFunction
 After Exclusive Locking 0x100100600 secondFunction
 Returned From Call 0x100100600 firstFunction
 Returning from 0x100100600 firstFunction
 Before Exclusive Locking 0x1001003b0 secondFunction
 After Exclusive Locking 0x1001003b0 secondFunction
 Returned From Call 0x1001003b0 firstFunction
 Returning from 0x1001003b0 firstFunction
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/4648#comment:3>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:07 UTC