From 7de8a2267f3e96392dce43136801a51d8b457903 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Thu, 24 Mar 2016 18:09:14 -0700 Subject: [PATCH] one more try --- interface/src/Application.cpp | 3 ++- libraries/shared/src/SimpleMovingAverage.h | 25 ++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 5e01bcbe88..22bc272921 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -321,8 +321,9 @@ public: static std::atomic _lastReport; static std::atomic _maxElapsed; static std::atomic _maxElapsedAverage; - bool _quit { false }; static ThreadSafeMovingAverage _movingAverage; + + bool _quit { false }; }; std::atomic DeadlockWatchdogThread::_heartbeat; diff --git a/libraries/shared/src/SimpleMovingAverage.h b/libraries/shared/src/SimpleMovingAverage.h index 67efa296f6..a35199a97c 100644 --- a/libraries/shared/src/SimpleMovingAverage.h +++ b/libraries/shared/src/SimpleMovingAverage.h @@ -14,7 +14,7 @@ #ifndef hifi_SimpleMovingAverage_h #define hifi_SimpleMovingAverage_h -#include +#include #include class SimpleMovingAverage { @@ -68,12 +68,17 @@ public: template class ThreadSafeMovingAverage { public: void clear() { + std::unique_lock lock(_lock); numSamples = 0; } - bool isAverageValid() const { return (numSamples > 0); } + bool isAverageValid() const { + std::unique_lock lock(_lock); + return (numSamples > 0); + } void addSample(T sample) { + std::unique_lock lock(_lock); if (numSamples > 0) { T lastAverage = average; average = (sample * WEIGHTING) + (lastAverage * ONE_MINUS_WEIGHTING); @@ -84,14 +89,22 @@ public: numSamples++; } - T getAverage() const { return average; } - T getNumSamples() const { return numSamples; } + T getAverage() const { + std::unique_lock lock(_lock); + return average; + } + + T getNumSamples() const { + std::unique_lock lock(_lock); + return numSamples; + } private: const float WEIGHTING = 1.0f / (float)MAX_NUM_SAMPLES; const float ONE_MINUS_WEIGHTING = 1.0f - WEIGHTING; - std::atomic numSamples{ 0 }; - std::atomic average; + int numSamples { 0 }; + T average; + mutable std::mutex _lock; };