My Project
atomicCounter.h
Go to the documentation of this file.
1/* atomicCounter.h
2 */
3#ifndef OSL_ATOMICCOUNTER_H
4#define OSL_ATOMICCOUNTER_H
5
6#include "osl/config.h"
7#include <atomic>
8#include <algorithm>
9
10namespace osl
11{
12 namespace misc
13 {
14 template <class Counter>
16 {
17 Counter& counter;
18 explicit IncrementLock(Counter& c) : counter(c)
19 {
20 counter.inc();
21 }
23 {
24 counter.dec();
25 }
26 IncrementLock(const IncrementLock&) = delete;
28 };
30 {
31 std::atomic<int> count;
32 public:
33 explicit AtomicCounter(int count_=0) {
34 this->count=count_;
35 }
36 void inc(){
37 count.fetch_add(1);
38 }
39 void inc(int value){
40 count.fetch_add(value);
41 }
43 return count.fetch_add(1);
44 }
45 void dec(){
46 count.fetch_sub(1);
47 }
48 void max(int val){
49 int x=count;
50 if(x<val){
51 while(! count.compare_exchange_weak(x,val) && x<val)
52 ;
53 }
54 }
55 int value() const{
56 return count;
57 }
58 void setValue(int value) {
59 count = value;
60 }
62 };
63 }
65}
66
67#endif /* OSL_ATOMICCOUNTER_H */
68// ;;; Local Variables:
69// ;;; mode:c++
70// ;;; c-basic-offset:2
71// ;;; End:
72
std::atomic< int > count
void setValue(int value)
IncrementLock< AtomicCounter > IncLock
AtomicCounter(int count_=0)
IncrementLock(const IncrementLock &)=delete
IncrementLock & operator=(const IncrementLock &)=delete