2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-20 16:32:08 +00:00
Files
asio/example/cpp14/executors/bank_account_1.cpp
2020-04-08 17:48:10 +10:00

55 lines
829 B
C++

#include <boost/asio/ts/executor.hpp>
#include <boost/asio/thread_pool.hpp>
#include <iostream>
using boost::asio::post;
using boost::asio::thread_pool;
// Traditional active object pattern.
// Member functions do not block.
class bank_account
{
int balance_ = 0;
mutable thread_pool pool_{1};
public:
void deposit(int amount)
{
post(pool_, [=]
{
balance_ += amount;
});
}
void withdraw(int amount)
{
post(pool_, [=]
{
if (balance_ >= amount)
balance_ -= amount;
});
}
void print_balance() const
{
post(pool_, [=]
{
std::cout << "balance = " << balance_ << "\n";
});
}
~bank_account()
{
pool_.join();
}
};
int main()
{
bank_account acct;
acct.deposit(20);
acct.withdraw(10);
acct.print_balance();
}