mirror of
https://github.com/usatiuk/backup.git
synced 2025-10-26 17:37:47 +01:00
proper cmake
This commit is contained in:
31
src/utils/srcs/RunningAverage.cpp
Normal file
31
src/utils/srcs/RunningAverage.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 05.05.2023.
|
||||
//
|
||||
|
||||
#include "../includes/RunningAverage.h"
|
||||
|
||||
RunningAverage::RunningAverage(std::function<unsigned long long int()> getFunc, int max, int ms)
|
||||
: getFunc(std::move(getFunc)), max(max), ms(ms), thread(&RunningAverage::loop, this) {
|
||||
}
|
||||
|
||||
void RunningAverage::loop() {
|
||||
while (!stop) {
|
||||
{
|
||||
std::lock_guard lock(dataLock);
|
||||
data.emplace_front(getFunc());
|
||||
if (data.size() > max) data.pop_back();
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::duration(std::chrono::milliseconds(ms)));
|
||||
}
|
||||
}
|
||||
|
||||
RunningAverage::~RunningAverage() {
|
||||
stop = true;
|
||||
thread.join();
|
||||
}
|
||||
|
||||
unsigned long long RunningAverage::get() {
|
||||
std::lock_guard lock(dataLock);
|
||||
if (data.empty()) return 0;
|
||||
return std::accumulate(data.begin(), data.end(), 0UL) / data.size();
|
||||
}
|
||||
Reference in New Issue
Block a user