atomic_counter.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _ATOMIC_COUNTER_H_
00023 #define _ATOMIC_COUNTER_H_
00024
00025 namespace dbp {
00026
00028
00033 class atomic_counter {
00034 public:
00036
00039 atomic_counter(): _counter(0) { }
00041
00046 atomic_counter(int value): _counter(value) { }
00048
00053 atomic_counter(const atomic_counter &src) {
00054 _counter = src._counter;
00055 }
00057 const atomic_counter& operator=(const atomic_counter &src) {
00058 _counter = src._counter;
00059 return *this;
00060 }
00062 inline void operator++() {
00063 #ifdef __GNUC__
00064 __sync_add_and_fetch(&_counter, 1);
00065 #endif
00066 }
00068 inline void operator--() {
00069 #ifdef __GNUC__
00070 __sync_sub_and_fetch(&_counter, 1);
00071 #endif
00072 }
00073 private:
00074 int _counter;
00075 };
00076
00077 }
00078
00079 #endif
00080