From e415feaac7be4e37d9465693330386cdeacc0e19 Mon Sep 17 00:00:00 2001 From: korenkonder Date: Wed, 28 Aug 2024 17:11:27 +0300 Subject: [PATCH] Use atomic in timer struct --- src/KKdLib/timer.cpp | 8 ++------ src/KKdLib/timer.hpp | 10 ++++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/KKdLib/timer.cpp b/src/KKdLib/timer.cpp index 8421bbfc..37fa992b 100644 --- a/src/KKdLib/timer.cpp +++ b/src/KKdLib/timer.cpp @@ -51,7 +51,6 @@ void timer::start_of_cycle() { if (history_counter >= HISTORY_COUNT) history_counter = 0; - std::unique_lock u_lock(freq_mtx); freq_hist = 1000.0 / (freq / HISTORY_COUNT); } @@ -70,17 +69,14 @@ void timer::end_of_cycle() { } double_t timer::get_freq() { - std::unique_lock u_lock(freq_mtx); return freq; } -void timer::set_freq(double_t freq) { - std::unique_lock u_lock(freq_mtx); - this->freq = freq; +void timer::set_freq(double_t value) { + freq = value; } double_t timer::get_freq_hist() { - std::unique_lock u_lock(freq_hist_mtx); return freq_hist; } diff --git a/src/KKdLib/timer.hpp b/src/KKdLib/timer.hpp index 75eb2e35..5f762841 100644 --- a/src/KKdLib/timer.hpp +++ b/src/KKdLib/timer.hpp @@ -7,7 +7,7 @@ #include "default.hpp" #include "waitable_timer.hpp" -#include +#include #define HISTORY_COUNT 0x08 @@ -17,10 +17,8 @@ struct timer { LARGE_INTEGER curr_time; LARGE_INTEGER prev_time; double_t inv_freq; - double_t freq; - double_t freq_hist; - std::mutex freq_mtx; - std::mutex freq_hist_mtx; + std::atomic freq; + std::atomic freq_hist; waitable_timer wait_timer; timer(double_t freq); @@ -29,7 +27,7 @@ struct timer { void start_of_cycle(); void end_of_cycle(); double_t get_freq(); - void set_freq(double_t freq); + void set_freq(double_t value); double_t get_freq_hist(); void reset(); void sleep(double_t msec);