OpenWalnut  1.5.0dev
WCounter.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WCOUNTER_H
26 #define WCOUNTER_H
27 
28 #include <shared_mutex>
29 
30 #include <boost/thread.hpp>
31 
32 /**
33  * This is a simple but thread-safe counter. Use it to handle counting between multiple threads. When used as static member, you can utilized
34  * WCounter for instance counting.
35  */
36 class WCounter
37 {
38 public:
39  /**
40  * Constructor. Counter starts at 0.
41  */
43  : m_counterMutex(),
44  m_counter( 0 )
45  {
46  }
47 
48  /**
49  * Increase count by 1 and return the new counter value. This is threadsafe.
50  *
51  * \return The new counter value.
52  */
54  {
55  std::unique_lock< std::shared_mutex > lock( m_counterMutex );
56  return ++m_counter;
57  }
58 
59  /**
60  * Decrease count by 1 and return the new counter value. This is threadsafe.
61  *
62  * \return The new counter value.
63  */
65  {
66  std::unique_lock< std::shared_mutex > lock( m_counterMutex );
67  return --m_counter;
68  }
69 
70  /**
71  * Get current count.
72  *
73  * \return the current count.
74  */
75  int operator() () const
76  {
77  boost::shared_lock< std::shared_mutex > lock( m_counterMutex );
78  return m_counter;
79  }
80 
81  /**
82  * Reset the counter to 0.
83  *
84  * \return The value the counter had before the reset.
85  */
86  int reset()
87  {
88  std::unique_lock< std::shared_mutex > lock( m_counterMutex );
89  int tmp = m_counter;
90  m_counter = 0;
91  return tmp;
92  }
93 
94 private:
95  //! No copy construction.
96  WCounter( WCounter& /* count */ );
97 
98  /**
99  * No copy operator.
100  *
101  * \return Nothing.
102  */
103  WCounter operator= ( WCounter& /* count */ );
104 
105  //! A mutex to protect the counter from concurrent updates.
106  mutable std::shared_mutex m_counterMutex;
107 
108  //! The counter.
110 };
111 
112 #endif // WCOUNTER_H
113 
This is a simple but thread-safe counter.
Definition: WCounter.h:37
int reset()
Reset the counter to 0.
Definition: WCounter.h:86
int operator()() const
Get current count.
Definition: WCounter.h:75
int operator++()
Increase count by 1 and return the new counter value.
Definition: WCounter.h:53
int m_counter
The counter.
Definition: WCounter.h:109
WCounter operator=(WCounter &)
No copy operator.
int operator--()
Decrease count by 1 and return the new counter value.
Definition: WCounter.h:64
WCounter()
Constructor.
Definition: WCounter.h:42
WCounter(WCounter &)
No copy construction.
std::shared_mutex m_counterMutex
A mutex to protect the counter from concurrent updates.
Definition: WCounter.h:106