forked from chriskohlhoff/executors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_account_1.cpp
54 lines (46 loc) · 852 Bytes
/
bank_account_1.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
51
52
53
54
#include <experimental/thread_pool>
#include <iostream>
using std::experimental::post;
using std::experimental::thread_pool;
// Traditional active object pattern.
// Member functions do not block.
class bank_account
{
int balance_ = 0;
thread_pool pool_{1};
mutable thread_pool::executor_type ex_ = pool_.get_executor();
public:
void deposit(int amount)
{
post(ex_, [=]
{
balance_ += amount;
});
}
void withdraw(int amount)
{
post(ex_, [=]
{
if (balance_ >= amount)
balance_ -= amount;
});
}
void print_balance() const
{
post(ex_, [=]
{
std::cout << "balance = " << balance_ << "\n";
});
}
~bank_account()
{
pool_.join();
}
};
int main()
{
bank_account acct;
acct.deposit(20);
acct.withdraw(10);
acct.print_balance();
}