Use atomic in timer struct

This commit is contained in:
korenkonder
2024-08-28 17:11:27 +03:00
parent 7465271662
commit e415feaac7
2 changed files with 6 additions and 12 deletions
+2 -6
View File
@@ -51,7 +51,6 @@ void timer::start_of_cycle() {
if (history_counter >= HISTORY_COUNT)
history_counter = 0;
std::unique_lock<std::mutex> 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<std::mutex> u_lock(freq_mtx);
return freq;
}
void timer::set_freq(double_t freq) {
std::unique_lock<std::mutex> 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<std::mutex> u_lock(freq_hist_mtx);
return freq_hist;
}
+4 -6
View File
@@ -7,7 +7,7 @@
#include "default.hpp"
#include "waitable_timer.hpp"
#include <mutex>
#include <atomic>
#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<double_t> freq;
std::atomic<double_t> 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);