implemented simple and atomic auto counters

This commit is contained in:
Dominik Tomičević 2015-07-31 12:35:14 +02:00
parent 4d1c92641a
commit a4d06be903
2 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,21 @@
#ifndef MEMGRAPH_UTILS_COUNTERS_ATOMIC_COUNTER_HPP
#define MEMGRAPH_UTILS_COUNTERS_ATOMIC_COUNTER_HPP
#include <atomic>
template <class T>
class AtomicCounter
{
public:
AtomicCounter(T initial) : counter(initial) {}
T next()
{
return counter.fetch_add(1, std::memory_order_relaxed);
}
private:
std::atomic<T> counter;
};
#endif

View File

@ -0,0 +1,24 @@
#ifndef MEMGRAPH_UTILS_COUNTERS_SIMPLE_COUNTER_HPP
#define MEMGRAPH_UTILS_COUNTERS_SIMPLE_COUNTER_HPP
template <class T>
class SimpleCounter
{
public:
SimpleCounter(T initial) : counter(initial) {}
T next()
{
return ++counter;
}
T count()
{
return counter;
}
private:
T counter;
};
#endif