]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/executors/bank_account_1.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / asio / example / cpp11 / executors / bank_account_1.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 do not block.
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 pool_.executor(),
21 [this, amount]
22 {
23 balance_ += amount;
24 });
25 }
26
27 void withdraw(int amount)
28 {
29 execution::execute(
30 pool_.executor(),
31 [this, amount]
32 {
33 if (balance_ >= amount)
34 balance_ -= amount;
35 });
36 }
37
38 void print_balance() const
39 {
40 execution::execute(
41 pool_.executor(),
42 [this]
43 {
44 std::cout << "balance = " << balance_ << "\n";
45 });
46 }
47
48 ~bank_account()
49 {
50 pool_.wait();
51 }
52 };
53
54 int main()
55 {
56 bank_account acct;
57 acct.deposit(20);
58 acct.withdraw(10);
59 acct.print_balance();
60 }