Sha256: 548fd9d6129f42ecfe39edb308a0c60feb3a4e22c39e37fdb2ff3e2386433d6a

Contents?: true

Size: 803 Bytes

Versions: 16

Compression:

Stored size: 803 Bytes

Contents

#include <asio/ts/executor.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

Version Path
couchbase-3.0.0.alpha.4-x86_64-linux ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.4-x86_64-darwin-19 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.4-universal-darwin-19 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.4 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.3-x86_64-linux ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.3-x86_64-darwin-19 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.3-universal-darwin-19 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.3 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.2-x86_64-linux ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.2-x86_64-darwin-19 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.2-universal-darwin-19 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.2 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.1-x86_64-linux ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.1-x86_64-darwin-19 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.1-universal-darwin-19 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp
couchbase-3.0.0.alpha.1 ext/third_party/asio/asio/src/examples/cpp14/executors/bank_account_1.cpp