nidas  v1.2-1520
MutexCount.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  Copyright 2005 UCAR, NCAR, All Rights Reserved
6 
7  $LastChangedDate: 2011-11-16 15:03:17 -0700 (Wed, 16 Nov 2011) $
8 
9  $LastChangedRevision: 6326 $
10 
11  $LastChangedBy: maclean $
12 
13  $HeadURL: http://svn.eol.ucar.edu/svn/nidas/trunk/src/nidas/util/MutexCount.h $
14 
15  ********************************************************************
16  */
17 
18 #ifndef NIDAS_UTIL_MUTEXCOUNT_H
19 #define NIDAS_UTIL_MUTEXCOUNT_H
20 
21 #include "ThreadSupport.h"
22 
23 namespace nidas { namespace util {
24 
29 template <typename T>
31 {
32 public:
33  explicit MutexCount(T value = 0) :
34  _lock(),
35  _value(value)
36  {}
37 
38  T
40  {
41  Synchronized lock(_lock);
42  return _value;
43  }
44 
45  operator T()
46  {
47  return value();
48  }
49 
51  MutexCount&
53  {
54  Synchronized lock(_lock);
55  ++_value;
56  return *this;
57  }
58 
60  MutexCount&
62  {
63  Synchronized lock(_lock);
64  --_value;
65  return *this;
66  }
67 
68 private:
70  T _value;
71 };
72 
73 
74 
75 }} // namespace nidas namespace util
76 
77 #endif
78 
Synchronized is used a simple guard object for critical sections.
Definition: ThreadSupport.h:544
T _value
Definition: MutexCount.h:70
T value()
Definition: MutexCount.h:39
Mutex _lock
Definition: MutexCount.h:69
MutexCount & operator--()
Pre decrement operator.
Definition: MutexCount.h:61
MutexCount(T value=0)
Definition: MutexCount.h:33
MutexCount & operator++()
Pre increment operator.
Definition: MutexCount.h:52
A class which wraps a numeric value and guards operations on it with a Mutex.
Definition: MutexCount.h:30
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154