-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelthread.impl.cpp
More file actions
132 lines (105 loc) · 2.8 KB
/
Copy pathmodelthread.impl.cpp
File metadata and controls
132 lines (105 loc) · 2.8 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <sstream>
#include <chrono>
ModelThread::ModelThread(SetupParameters& setup_params_in) :
m_Mutex(),
m_shall_stop(false),
m_has_stopped(false),
m_fraction_done(0.0),
m_message(),
model(setup_params_in)
{
}
// Accesses to these data are synchronized by a mutex.
// Some microseconds can be saved by getting all data at once, instead of having
// separate get_fraction_done() and get_message() methods.
void ModelThread::get_data(double* fraction_done, Glib::ustring* message) const
{
std::lock_guard<std::mutex> lock(m_Mutex);
if (fraction_done)
*fraction_done = m_fraction_done;
if (message)
*message = m_message;
}
void ModelThread::stop_work()
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_shall_stop = true;
}
bool ModelThread::has_stopped() const
{
std::lock_guard<std::mutex> lock(m_Mutex);
return m_has_stopped;
}
void ModelThread::do_one_turn(CivWindow* caller)
{
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_has_stopped = false;
m_message = "";
} // The mutex is unlocked here by lock's destructor.
model.advance();
model.i_turn++;
if (model.p.person.size()==0) {printf("\nEnding simulation early...");}
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_fraction_done = float(model.i_turn)/float(model.n_turns);
}
caller->notify();
// Finished
if (model.i_turn > model.n_turns) model.conclusions();
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_shall_stop = false;
m_has_stopped = true;
}
caller->notify();
}
void ModelThread::do_work(CivWindow* caller)
{
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_has_stopped = false;
m_fraction_done = 0.0;
m_message = "";
} // The mutex is unlocked here by lock's destructor.
int next_milestone=0;
int milestone_dist=5;
// Advance to next turn
while (model.i_turn <= model.n_turns)
{
model.advance();
model.i_turn++;
if (model.p.person.size()==0) {printf("\nEnding simulation early..."); break;}
// std::this_thread::sleep_for(std::chrono::milliseconds(1000));
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_fraction_done = float(model.i_turn)/float(model.n_turns);
if (int(100*m_fraction_done) >= next_milestone)
{
std::ostringstream ostr;
ostr << next_milestone << "% done\n";
m_message += ostr.str();
next_milestone+=milestone_dist;
}
if (m_fraction_done >= 1.0)
{
m_message += "Finished";
break;
}
if (m_shall_stop)
{
m_message += "Stopped";
break;
}
}
caller->notify();
}
// Finished
if (model.i_turn > model.n_turns) model.conclusions();
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_shall_stop = false;
m_has_stopped = true;
}
caller->notify();
}