Eclipse SUMO - Simulation of Urban MObility
FXConditionalLock.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2018-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // A scoped lock which only triggers on condition
19 /****************************************************************************/
20 
21 #ifndef FXConditionalLock_h
22 #define FXConditionalLock_h
23 #include <config.h>
24 
25 #include <fx.h>
26 
27 
28 // ===========================================================================
29 // class definitions
30 // ===========================================================================
36 public:
38  FXConditionalLock(FXMutex& m, const bool condition)
39  : myMutex(m), myCondition(condition) {
40  if (condition) {
41  m.lock();
42  }
43  }
44 
47  if (myCondition) {
48  myMutex.unlock();
49  }
50  }
51 
52 private:
53  FXMutex& myMutex;
54  const bool myCondition;
55 
56 private:
58 };
59 
60 
61 #endif
A scoped lock which only triggers on condition.
~FXConditionalLock()
Destroy and unlock associated mutex.
FXConditionalLock & operator=(const FXConditionalLock &)=delete
FXConditionalLock(FXMutex &m, const bool condition)
Construct & lock associated mutex if the condition is true.