-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_account.cpp
50 lines (39 loc) · 1011 Bytes
/
bank_account.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#define BOOST_ASIO_NO_DEPRECATED
#include <boost/config.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/asio/use_future.hpp>
#include <iostream>
using boost::asio::post;
using boost::asio::thread_pool;
using boost::asio::use_future;
// Traditional active object pattern.
// Member functions block until operation is finished.
class bank_account {
int balance_ = 0;
mutable thread_pool pool_ { 1 };
public:
void deposit(int amount)
{
post(pool_, use_future([=] { balance_ += amount; })).get();
}
void withdraw(int amount)
{
post(pool_, use_future([=] {
if (balance_ >= amount)
balance_ -= amount;
})).get();
}
int balance() const
{
return post(pool_, use_future([=] { return balance_; })).get();
}
};
int main()
{
bank_account acct;
acct.deposit(20);
acct.withdraw(10);
std::cout << "balance = " << acct.balance() << "\n";
return 0;
}