mirror of
https://github.com/boostorg/asio.git
synced 2025-05-12 14:11:39 +00:00
55 lines
1.1 KiB
C++
55 lines
1.1 KiB
C++
#include <boost/asio/execution.hpp>
|
|
#include <boost/asio/static_thread_pool.hpp>
|
|
#include <iostream>
|
|
|
|
using boost::asio::static_thread_pool;
|
|
namespace execution = boost::asio::execution;
|
|
|
|
// Traditional active object pattern.
|
|
// Member functions block until operation is finished.
|
|
|
|
class bank_account
|
|
{
|
|
int balance_ = 0;
|
|
mutable static_thread_pool pool_{1};
|
|
|
|
public:
|
|
void deposit(int amount)
|
|
{
|
|
boost::asio::require(pool_.executor(), execution::blocking.always).execute(
|
|
[this, amount]
|
|
{
|
|
balance_ += amount;
|
|
});
|
|
}
|
|
|
|
void withdraw(int amount)
|
|
{
|
|
boost::asio::require(pool_.executor(), execution::blocking.always).execute(
|
|
[this, amount]
|
|
{
|
|
if (balance_ >= amount)
|
|
balance_ -= amount;
|
|
});
|
|
}
|
|
|
|
int balance() const
|
|
{
|
|
int result = 0;
|
|
boost::asio::require(pool_.executor(), execution::blocking.always).execute(
|
|
[this, &result]
|
|
{
|
|
result = balance_;
|
|
});
|
|
return result;
|
|
}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
bank_account acct;
|
|
acct.deposit(20);
|
|
acct.withdraw(10);
|
|
std::cout << "balance = " << acct.balance() << "\n";
|
|
}
|