summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp14/executors/bank_account_1.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/asio/src/examples/cpp14/executors/bank_account_1.cpp')
-rw-r--r--3rdparty/asio/src/examples/cpp14/executors/bank_account_1.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp14/executors/bank_account_1.cpp b/3rdparty/asio/src/examples/cpp14/executors/bank_account_1.cpp
new file mode 100644
index 00000000000..abfd16a7ee0
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp14/executors/bank_account_1.cpp
@@ -0,0 +1,60 @@
+#include <asio/execution.hpp>
+#include <asio/static_thread_pool.hpp>
+#include <iostream>
+
+using asio::static_thread_pool;
+namespace execution = asio::execution;
+
+// Traditional active object pattern.
+// Member functions do not block.
+
+class bank_account
+{
+ int balance_ = 0;
+ mutable static_thread_pool pool_{1};
+
+public:
+ void deposit(int amount)
+ {
+ execution::execute(
+ pool_.executor(),
+ [this, amount]
+ {
+ balance_ += amount;
+ });
+ }
+
+ void withdraw(int amount)
+ {
+ execution::execute(
+ pool_.executor(),
+ [this, amount]
+ {
+ if (balance_ >= amount)
+ balance_ -= amount;
+ });
+ }
+
+ void print_balance() const
+ {
+ execution::execute(
+ pool_.executor(),
+ [this]
+ {
+ std::cout << "balance = " << balance_ << "\n";
+ });
+ }
+
+ ~bank_account()
+ {
+ pool_.wait();
+ }
+};
+
+int main()
+{
+ bank_account acct;
+ acct.deposit(20);
+ acct.withdraw(10);
+ acct.print_balance();
+}