-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sun.cpp
60 lines (47 loc) · 1.36 KB
/
Sun.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
#include <iostream>
#include "Sun.h"
#include "Utils/Utils.h"
Sun::Sun(Meadow &meadow) : meadow(meadow), live_thread(&Sun::shine, this), is_day(true), progress(0) {}
Sun::~Sun() {
if (live_thread.joinable()) {
live_thread.join();
}
}
void Sun::day() {
Utils::thread_safe_cout("===== DAY =====");
is_day = true;
thread_local std::uniform_int_distribution<> wait(10, 24);
int time = wait(random_generator);
for (int i = time, counter = 0; i > 0; --i) {
progress = Utils::get_percentage(counter, time);
counter++;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
progress = 0;
}
void Sun::night() {
thread_local std::uniform_int_distribution<> wait(10, 24);
is_day = false;
Utils::thread_safe_cout("==== NIGHT ====");
int time = wait(random_generator);
for (int i = time, counter = 0; i > 0; --i) {
progress = Utils::get_percentage(counter, time);
counter++;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
progress = 0;
}
void Sun::shine() {
meadow.synchronization.setSleep(true);
meadow.synchronization.wait();
do {
day();
night();
} while (meadow.ready);
}
const std::atomic<bool> &Sun::getIsDay() const {
return is_day;
}
const std::atomic_int &Sun::getProgress() const {
return progress;
}