-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cpp
80 lines (61 loc) · 1.84 KB
/
Database.cpp
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
79
80
#include "Database.h"
Database::Database(SharedParameters* ptr_param){
if (ptr_param != nullptr) {
ptr_parameters = ptr_param;
}
else
throw;
}
void Database::processSequence(CommunicationPipe& comm) {
int val = 0;
int sz = 0;
std::ofstream outputFile(writeFilename);
if (outputFile.fail()) {
std::cerr << "Can't open output file" << std::endl;
exit(1);
}
std::chrono::steady_clock clock;
auto time_start = clock.now();
auto time_point = time_start;
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(time_start - time_point);
while (true) {
std::unique_lock<std::mutex> locker(comm.mu_pipe);
comm.cond_pipe.wait(locker, [&]() { return !comm.pipe.empty(); });
val = comm.pipe.front();
comm.pipe.pop_front();
sz = comm.pipe.size(); //needed?
locker.unlock();
updateParameters();
data.push_back(val);
if (data.size() == parameters_state.M) {
dtb.push_back(data);
data.clear();
time_start = clock.now();
}
else {
time_point = clock.now();
time_span = time_point - time_start;
if (time_span.count() >= parameters_state.T) {
dtb.push_back(data);
data.clear();
time_start = clock.now();
}
}
if (dtb.size() == ptr_parameters->N) {
for (const auto& i : dtb[0])
outputFile << i << " ";
outputFile << "\nParameters: R=" << ptr_parameters->R
<< " T=" << ptr_parameters->T << " M=" << ptr_parameters->M
<< " N=" << ptr_parameters->N << "\nTime: \n"
<< "******************************************" << std::endl;
dtb.pop_front();
}
}
}
void Database::updateParameters() {
std::lock_guard<std::mutex> locker(ptr_parameters->mu_param);
parameters_state = *ptr_parameters;
}
Database::~Database(){
ptr_parameters = nullptr;
}