-
Notifications
You must be signed in to change notification settings - Fork 72
/
bank_account_3.cpp
55 lines (48 loc) · 1.02 KB
/
bank_account_3.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
55
#include <experimental/executor>
#include <experimental/future>
#include <experimental/strand>
#include <iostream>
using std::experimental::dispatch;
using std::experimental::package;
using std::experimental::strand;
using std::experimental::system_executor;
// Active object sharing a system-wide pool of threads.
// Member functions block until operation is finished.
class bank_account
{
int balance_ = 0;
mutable strand<system_executor> strand_;
public:
void deposit(int amount)
{
dispatch(strand_,
package([=]
{
balance_ += amount;
})).get();
}
void withdraw(int amount)
{
dispatch(strand_,
package([=]
{
if (balance_ >= amount)
balance_ -= amount;
})).get();
}
int balance() const
{
return dispatch(strand_,
package([=]
{
return balance_;
})).get();
}
};
int main()
{
bank_account acct;
acct.deposit(20);
acct.withdraw(10);
std::cout << "balance = " << acct.balance() << "\n";
}