]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/asio/example/cpp14/executors/bank_account_2.cpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / libs / asio / example / cpp14 / executors / bank_account_2.cpp
diff --git a/ceph/src/boost/libs/asio/example/cpp14/executors/bank_account_2.cpp b/ceph/src/boost/libs/asio/example/cpp14/executors/bank_account_2.cpp
new file mode 100644 (file)
index 0000000..8de9a3c
--- /dev/null
@@ -0,0 +1,53 @@
+#include <boost/asio/ts/executor.hpp>
+#include <boost/asio/thread_pool.hpp>
+#include <iostream>
+
+using boost::asio::post;
+using boost::asio::thread_pool;
+using boost::asio::use_future;
+
+// Traditional active object pattern.
+// Member functions block until operation is finished.
+
+class bank_account
+{
+  int balance_ = 0;
+  mutable thread_pool pool_{1};
+
+public:
+  void deposit(int amount)
+  {
+    post(pool_,
+      use_future([=]
+        {
+          balance_ += amount;
+        })).get();
+  }
+
+  void withdraw(int amount)
+  {
+    post(pool_,
+      use_future([=]
+        {
+          if (balance_ >= amount)
+            balance_ -= amount;
+        })).get();
+  }
+
+  int balance() const
+  {
+    return post(pool_,
+      use_future([=]
+        {
+          return balance_;
+        })).get();
+  }
+};
+
+int main()
+{
+  bank_account acct;
+  acct.deposit(20);
+  acct.withdraw(10);
+  std::cout << "balance = " << acct.balance() << "\n";
+}