implemented simple and atomic auto counters
This commit is contained in:
parent
4d1c92641a
commit
a4d06be903
utils/counters
21
utils/counters/atomic_counter.hpp
Normal file
21
utils/counters/atomic_counter.hpp
Normal 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
|
24
utils/counters/simple_counter.hpp
Normal file
24
utils/counters/simple_counter.hpp
Normal 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
|
Loading…
Reference in New Issue
Block a user