forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderivablecounter.h
36 lines (28 loc) · 861 Bytes
/
derivablecounter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#ifndef DERIVABLECOUNTER_H
#define DERIVABLECOUNTER_H
#include <chrono>
#include <mutex>
/**
* @brief The DerivableCounter is a counter which can derive val/dt.
*
* It's not thread-safe, to avoid unnecessary locking. You should have counters per thread.
*/
class DerivableCounter
{
uint64_t val = 0;
uint64_t valPrevious = 0;
std::chrono::time_point<std::chrono::steady_clock> timeOfPrevious = std::chrono::steady_clock::now();
std::mutex timeMutex;
public:
void inc(uint64_t n = 1);
uint64_t get() const;
double getPerSecond();
};
#endif // DERIVABLECOUNTER_H