]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/executors/bank_account_2.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / asio / example / cpp11 / executors / bank_account_2.cpp
1 #include <boost/asio/execution.hpp>
2 #include <boost/asio/static_thread_pool.hpp>
3 #include <iostream>
4
5 using boost::asio::static_thread_pool;
6 namespace execution = boost::asio::execution;
7
8 // Traditional active object pattern.
9 // Member functions block until operation is finished.
10
11 class bank_account
12 {
13 int balance_ = 0;
14 mutable static_thread_pool pool_{1};
15
16 public:
17 void deposit(int amount)
18 {
19 execution::execute(
20 boost::asio::require(pool_.executor(),
21 execution::blocking.always),
22 [this, amount]
23 {
24 balance_ += amount;
25 });
26 }
27
28 void withdraw(int amount)
29 {
30 execution::execute(
31 boost::asio::require(pool_.executor(),
32 execution::blocking.always),
33 [this, amount]
34 {
35 if (balance_ >= amount)
36 balance_ -= amount;
37 });
38 }
39
40 int balance() const
41 {
42 int result = 0;
43 execution::execute(
44 boost::asio::require(pool_.executor(),
45 execution::blocking.always),
46 [this, &result]
47 {
48 result = balance_;
49 });
50 return result;
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 }