-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.hpp
59 lines (47 loc) · 1.71 KB
/
task.hpp
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
#pragma once
#ifndef __HPC_TASK_HPP__
#define __HPC_TASK_HPP__
#include <chrono>
namespace hpc {
class task_base {
public:
bool status;
double task_time;
task_base() {
this->status = false;
};
virtual void process() = 0;
virtual void run() final {
auto start = std::chrono::system_clock::now();
this->process();
this->status = true;
auto end = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
this->task_time = double(duration.count())
* std::chrono::microseconds::period::num
/ std::chrono::microseconds::period::den
* 1000;
};
virtual bool wait(double wait_ms=0) final {
if (wait_ms == 0) {
while (!this->status) {};
return true;
} else {
auto start = std::chrono::system_clock::now();
while (!this->status) {
auto end = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
auto delay_ms = double(duration.count())
* std::chrono::microseconds::period::num
/ std::chrono::microseconds::period::den
* 1000;
if (delay_ms >= wait_ms) {
return false;
}
};
return true;
}
};
};
}
#endif // __HPC_TASK_HPP__