summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/thread/example/ba_externallly_locked.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/thread/example/ba_externallly_locked.cpp
parentInitial commit. (diff)
downloadceph-upstream.tar.xz
ceph-upstream.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/thread/example/ba_externallly_locked.cpp')
-rw-r--r--src/boost/libs/thread/example/ba_externallly_locked.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/boost/libs/thread/example/ba_externallly_locked.cpp b/src/boost/libs/thread/example/ba_externallly_locked.cpp
new file mode 100644
index 00000000..e9387676
--- /dev/null
+++ b/src/boost/libs/thread/example/ba_externallly_locked.cpp
@@ -0,0 +1,120 @@
+// Copyright (C) 2012 Vicente Botet
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#define BOOST_THREAD_VERSION 4
+
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/lockable_adapter.hpp>
+#include <boost/thread/externally_locked.hpp>
+#include <boost/thread/strict_lock.hpp>
+#include <boost/thread/lock_types.hpp>
+#include <iostream>
+
+#ifdef BOOST_MSVC
+# pragma warning(disable: 4355) // 'this' : used in base member initializer list
+#endif
+
+using namespace boost;
+
+class BankAccount
+{
+ int balance_;
+public:
+ void Deposit(int amount)
+ {
+ balance_ += amount;
+ }
+ void Withdraw(int amount)
+ {
+ balance_ -= amount;
+ }
+ int GetBalance()
+ {
+ return balance_;
+ }
+};
+
+//[AccountManager
+class AccountManager: public basic_lockable_adapter<mutex>
+{
+public:
+ typedef basic_lockable_adapter<mutex> lockable_base_type;
+ AccountManager() :
+ lockable_base_type(), checkingAcct_(*this), savingsAcct_(*this)
+ {
+ }
+ inline void Checking2Savings(int amount);
+ inline void AMoreComplicatedChecking2Savings(int amount);
+private:
+ /*<-*/
+ bool some_condition()
+ {
+ return true;
+ } /*->*/
+ externally_locked<BankAccount, AccountManager > checkingAcct_;
+ externally_locked<BankAccount, AccountManager > savingsAcct_;
+};
+//]
+
+//[Checking2Savings
+void AccountManager::Checking2Savings(int amount)
+{
+ strict_lock<AccountManager> guard(*this);
+ checkingAcct_.get(guard).Withdraw(amount);
+ savingsAcct_.get(guard).Deposit(amount);
+}
+//]
+
+//#if DO_NOT_COMPILE
+////[AMoreComplicatedChecking2Savings_DO_NOT_COMPILE
+//void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
+// unique_lock<AccountManager> guard(*this);
+// if (some_condition()) {
+// guard.lock();
+// }
+// checkingAcct_.get(guard).Withdraw(amount);
+// savingsAcct_.get(guard).Deposit(amount);
+// guard1.unlock();
+//}
+////]
+//#elif DO_NOT_COMPILE_2
+////[AMoreComplicatedChecking2Savings_DO_NOT_COMPILE2
+//void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
+// unique_lock<AccountManager> guard1(*this);
+// if (some_condition()) {
+// guard1.lock();
+// }
+// {
+// strict_lock<AccountManager> guard(guard1);
+// checkingAcct_.get(guard).Withdraw(amount);
+// savingsAcct_.get(guard).Deposit(amount);
+// }
+// guard1.unlock();
+//}
+////]
+//#else
+////[AMoreComplicatedChecking2Savings
+void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
+ unique_lock<AccountManager> guard1(*this);
+ if (some_condition()) {
+ guard1.lock();
+ }
+ {
+ nested_strict_lock<unique_lock<AccountManager> > guard(guard1);
+ checkingAcct_.get(guard).Withdraw(amount);
+ savingsAcct_.get(guard).Deposit(amount);
+ }
+ guard1.unlock();
+}
+////]
+//#endif
+
+int main()
+{
+ AccountManager mgr;
+ mgr.Checking2Savings(100);
+ return 0;
+}
+