-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtimer.h
More file actions
78 lines (63 loc) · 1.66 KB
/
timer.h
File metadata and controls
78 lines (63 loc) · 1.66 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Created by Ravil Galiev on 25.07.2023.
//
#pragma once
#include <thread>
#include <string>
#include "globals_extern.h"
#include "plaf.h"
#include "workloads/stop_condition/stop_condition.h"
#include "nlohmann/json.hpp"
namespace microbench::workload {
class Timer : public StopCondition {
PAD;
volatile bool stop_;
PAD;
std::thread* stop_thread_;
PAD;
volatile bool is_started_;
PAD;
public:
void wait() {
is_started_ = true;
std::this_thread::sleep_for(std::chrono::milliseconds(workTime));
stop_ = true;
}
public:
size_t workTime;
explicit Timer(size_t work_time = 10000)
: workTime(work_time),
stop_(true) {
}
Timer& set_work_time(size_t work_time) {
Timer::workTime = work_time;
return *this;
}
void start(size_t num_threads) override {
stop_ = false;
is_started_ = false;
stop_thread_ = new std::thread(&Timer::wait, this);
while (!is_started_) {
};
}
void clean() override {
stop_thread_->join();
delete stop_thread_;
}
bool is_stopped(int id) override {
return stop_;
}
void to_json(nlohmann::json& j) const override {
j["ClassName"] = "Timer";
j["workTime"] = workTime;
}
void from_json(const nlohmann::json& j) override {
workTime = j["workTime"];
}
~Timer() override = default;
std::string to_string(size_t indents = 1) override {
return indented_title_with_str_data("Type", "Timer", indents) +
indented_title_with_data("work time", workTime, indents);
}
};
} // namespace microbench::workload