]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (C) 2012 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 | #define BOOST_THREAD_VERSION 4 | |
7 | ||
8 | #include <boost/thread/mutex.hpp> | |
9 | #include <boost/thread/lockable_adapter.hpp> | |
10 | #include <boost/thread/externally_locked.hpp> | |
11 | #include <boost/thread/strict_lock.hpp> | |
12 | #include <boost/thread/lock_types.hpp> | |
13 | #include <iostream> | |
14 | ||
15 | using namespace boost; | |
16 | ||
17 | class BankAccount | |
18 | { | |
19 | int balance_; | |
20 | public: | |
21 | void Deposit(int amount) | |
22 | { | |
23 | balance_ += amount; | |
24 | } | |
25 | void Withdraw(int amount) | |
26 | { | |
27 | balance_ -= amount; | |
28 | } | |
29 | int GetBalance() | |
30 | { | |
31 | return balance_; | |
32 | } | |
33 | }; | |
34 | ||
35 | //[AccountManager | |
36 | class AccountManager: public basic_lockable_adapter<mutex> | |
37 | { | |
38 | public: | |
39 | typedef basic_lockable_adapter<mutex> lockable_base_type; | |
40 | AccountManager() : | |
41 | lockable_base_type(), checkingAcct_(*this), savingsAcct_(*this) | |
42 | { | |
43 | } | |
44 | inline void Checking2Savings(int amount); | |
45 | inline void AMoreComplicatedChecking2Savings(int amount); | |
46 | private: | |
47 | /*<-*/ | |
48 | bool some_condition() | |
49 | { | |
50 | return true; | |
51 | } /*->*/ | |
52 | externally_locked<BankAccount, AccountManager > checkingAcct_; | |
53 | externally_locked<BankAccount, AccountManager > savingsAcct_; | |
54 | }; | |
55 | //] | |
56 | ||
57 | //[Checking2Savings | |
58 | void AccountManager::Checking2Savings(int amount) | |
59 | { | |
60 | strict_lock<AccountManager> guard(*this); | |
61 | checkingAcct_.get(guard).Withdraw(amount); | |
62 | savingsAcct_.get(guard).Deposit(amount); | |
63 | } | |
64 | //] | |
65 | ||
66 | //#if DO_NOT_COMPILE | |
67 | ////[AMoreComplicatedChecking2Savings_DO_NOT_COMPILE | |
68 | //void AccountManager::AMoreComplicatedChecking2Savings(int amount) { | |
69 | // unique_lock<AccountManager> guard(*this); | |
70 | // if (some_condition()) { | |
71 | // guard.lock(); | |
72 | // } | |
73 | // checkingAcct_.get(guard).Withdraw(amount); | |
74 | // savingsAcct_.get(guard).Deposit(amount); | |
75 | // guard1.unlock(); | |
76 | //} | |
77 | ////] | |
78 | //#elif DO_NOT_COMPILE_2 | |
79 | ////[AMoreComplicatedChecking2Savings_DO_NOT_COMPILE2 | |
80 | //void AccountManager::AMoreComplicatedChecking2Savings(int amount) { | |
81 | // unique_lock<AccountManager> guard1(*this); | |
82 | // if (some_condition()) { | |
83 | // guard1.lock(); | |
84 | // } | |
85 | // { | |
86 | // strict_lock<AccountManager> guard(guard1); | |
87 | // checkingAcct_.get(guard).Withdraw(amount); | |
88 | // savingsAcct_.get(guard).Deposit(amount); | |
89 | // } | |
90 | // guard1.unlock(); | |
91 | //} | |
92 | ////] | |
93 | //#else | |
94 | ////[AMoreComplicatedChecking2Savings | |
95 | void AccountManager::AMoreComplicatedChecking2Savings(int amount) { | |
96 | unique_lock<AccountManager> guard1(*this); | |
97 | if (some_condition()) { | |
98 | guard1.lock(); | |
99 | } | |
100 | { | |
101 | nested_strict_lock<unique_lock<AccountManager> > guard(guard1); | |
102 | checkingAcct_.get(guard).Withdraw(amount); | |
103 | savingsAcct_.get(guard).Deposit(amount); | |
104 | } | |
105 | guard1.unlock(); | |
106 | } | |
107 | ////] | |
108 | //#endif | |
109 | ||
110 | int main() | |
111 | { | |
112 | AccountManager mgr; | |
113 | mgr.Checking2Savings(100); | |
114 | return 0; | |
115 | } | |
116 |