]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | #include <boost/asio/execution.hpp> |
2 | #include <boost/asio/static_thread_pool.hpp> | |
f67539c2 TL |
3 | #include <iostream> |
4 | ||
20effc67 TL |
5 | using boost::asio::static_thread_pool; |
6 | namespace execution = boost::asio::execution; | |
f67539c2 TL |
7 | |
8 | // Traditional active object pattern. | |
9 | // Member functions block until operation is finished. | |
10 | ||
11 | class bank_account | |
12 | { | |
13 | int balance_ = 0; | |
20effc67 | 14 | mutable static_thread_pool pool_{1}; |
f67539c2 TL |
15 | |
16 | public: | |
17 | void deposit(int amount) | |
18 | { | |
20effc67 TL |
19 | execution::execute( |
20 | boost::asio::require(pool_.executor(), | |
21 | execution::blocking.always), | |
22 | [this, amount] | |
f67539c2 TL |
23 | { |
24 | balance_ += amount; | |
20effc67 | 25 | }); |
f67539c2 TL |
26 | } |
27 | ||
28 | void withdraw(int amount) | |
29 | { | |
20effc67 TL |
30 | execution::execute( |
31 | boost::asio::require(pool_.executor(), | |
32 | execution::blocking.always), | |
33 | [this, amount] | |
f67539c2 TL |
34 | { |
35 | if (balance_ >= amount) | |
36 | balance_ -= amount; | |
20effc67 | 37 | }); |
f67539c2 TL |
38 | } |
39 | ||
40 | int balance() const | |
41 | { | |
20effc67 TL |
42 | int result = 0; |
43 | execution::execute( | |
44 | boost::asio::require(pool_.executor(), | |
45 | execution::blocking.always), | |
46 | [this, &result] | |
f67539c2 | 47 | { |
20effc67 TL |
48 | result = balance_; |
49 | }); | |
50 | return result; | |
f67539c2 TL |
51 | } |
52 | }; | |
53 | ||
54 | int main() | |
55 | { | |
56 | bank_account acct; | |
57 | acct.deposit(20); | |
58 | acct.withdraw(10); | |
59 | std::cout << "balance = " << acct.balance() << "\n"; | |
60 | } |