1 | // Copyright (C) 2010 Vicente Botet |
---|---|
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #include <iostream> |
7 | #include <boost/thread.hpp> |
8 | #include <boost/current_function.hpp> |
9 | |
10 | class boostThreadLocksTest |
11 | { |
12 | public: |
13 | boost::shared_mutex myMutex; |
14 | //boost::upgrade_lock<boost::shared_mutex> myLock; |
15 | static int firstFunction(boostThreadLocksTest *pBoostThreadLocksTest); |
16 | static int secondFunction(boostThreadLocksTest *pBoostThreadLocksTest, |
17 | boost::upgrade_lock<boost::shared_mutex>& upgr); |
18 | boostThreadLocksTest() |
19 | :myMutex() |
20 | //, myLock(myMutex,boost::defer_lock_t()) |
21 | {} |
22 | }; |
23 | |
24 | int boostThreadLocksTest::firstFunction(boostThreadLocksTest *pBoostThreadLocksTest) |
25 | { |
26 | std::cout<<"Entering "<<boost::this_thread::get_id()<< " "<< "firstFunction"<<std::endl; |
27 | boost::upgrade_lock<boost::shared_mutex> myLock(pBoostThreadLocksTest->myMutex); |
28 | pBoostThreadLocksTest->secondFunction(pBoostThreadLocksTest, upgr&: myLock); |
29 | std::cout<<"Returned From Call "<<boost::this_thread::get_id()<< " "<< "firstFunction"<<std::endl; |
30 | std::cout<<"Returning from "<<boost::this_thread::get_id()<< " "<< "firstFunction"<<std::endl; |
31 | return(0); |
32 | } |
33 | int boostThreadLocksTest::secondFunction(boostThreadLocksTest *, boost::upgrade_lock<boost::shared_mutex>& upgr) { |
34 | std::cout<<"Before Exclusive Locking "<<boost::this_thread::get_id()<< " "<< "secondFunction"<<std::endl; |
35 | boost::upgrade_to_unique_lock<boost::shared_mutex> localUniqueLock(upgr); |
36 | std::cout<<"After Exclusive Locking "<<boost::this_thread::get_id()<< " "<< "secondFunction"<<std::endl; |
37 | return(0); |
38 | } |
39 | int main() { |
40 | boostThreadLocksTest myObject; |
41 | boost::thread_group myThreadGroup; |
42 | myThreadGroup.create_thread(threadfunc: boost::bind(f: boostThreadLocksTest::firstFunction,a1: &myObject)); |
43 | myThreadGroup.create_thread(threadfunc: boost::bind(f: boostThreadLocksTest::firstFunction,a1: &myObject)); |
44 | myThreadGroup.create_thread(threadfunc: boost::bind(f: boostThreadLocksTest::firstFunction,a1: &myObject)); |
45 | myThreadGroup.join_all(); |
46 | return 0; |
47 | } |
48 |