Sha256: 40ef93e9b5e23859ee21a5083f95dbcf29f1e28922742689506e308bf991e5ff
Contents?: true
Size: 796 Bytes
Versions: 16
Compression:
Stored size: 796 Bytes
Contents
#include <asio/post.hpp> #include <asio/thread_pool.hpp> #include <iostream> using asio::post; using 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(); }
Version data entries
16 entries across 16 versions & 1 rubygems