MeVisLab Toolbox Reference
mlBarrier.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2009, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_BARRIER_H
14 #define ML_BARRIER_H
15 
18 
19 #include "mlUtilsSystem.h"
20 
21 #include "mlMutex.h"
22 #include "mlWaitCondition.h"
23 
24 ML_UTILS_START_NAMESPACE
25 
26 //----------------------------------------------------------------------------------
30 //----------------------------------------------------------------------------------
32 
33 public:
34  //----------------------------------------------------------------------------------
36  //----------------------------------------------------------------------------------
37  Barrier(unsigned int count = 0);
38 
39  //----------------------------------------------------------------------------------
42  //----------------------------------------------------------------------------------
43  void setNumberOfThreads(unsigned int count);
44 
45  //----------------------------------------------------------------------------------
48  //----------------------------------------------------------------------------------
49  bool wait();
50 
51 private:
53  Barrier(const Barrier&);
54  Barrier& operator =(const Barrier&);
55 
57  unsigned int _count;
59  unsigned int _generation;
61  unsigned int _numberOfThreads;
62 
64  Mutex _mutex;
66  WaitCondition _finishedNotifier;
67 };
68 
69 //----------------------------------------------------------------------------------
78 //----------------------------------------------------------------------------------
80 
81 public:
82  //----------------------------------------------------------------------------------
85  //----------------------------------------------------------------------------------
86  ScopedBarrierWaiter(Barrier& barrier, unsigned int numberOfExpectedWaits = 1);
88 
89  //----------------------------------------------------------------------------------
92  //----------------------------------------------------------------------------------
93  void wait();
94 
95 private:
98  ScopedBarrierWaiter& operator =(const ScopedBarrierWaiter&);
99 
101  Barrier& _barrier;
103  unsigned int _numberOfExpectedWaits;
104 };
105 
106 ML_UTILS_END_NAMESPACE
107 
108 #endif // __mlBarrier_H
109 
110 
A barrier class that handles synchronization of multiple threads Thread-safety: This class is thread-...
Definition: mlBarrier.h:31
Barrier(unsigned int count=0)
Creates a barrier with initial count of threads to synchronize.
bool wait()
Waits for other threads to also wait.
void setNumberOfThreads(unsigned int count)
Sets the number of threads that should synchronize with the wait call.
A helper class that ensures barrier waiting even when an exception occurs.
Definition: mlBarrier.h:79
ScopedBarrierWaiter(Barrier &barrier, unsigned int numberOfExpectedWaits=1)
Creates a ScopedBarrierWaiter that ensures that the barrier gets numberOfExpectedWaits calls to Barri...
void wait()
Waits for the barrier (this will prevent the ScopedBarrierWaiter from waiting in its destructor).
WaitCondition implements a wait condition for thread synchronization.
boost::mutex Mutex
Defines a non-recursive mutex.
Definition: mlMutex.h:39
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition: mlUtilities.h:20