Updated ProgressBar API to check if completed. Updated tick() method

This commit is contained in:
Pranav Srinivas KumaR
2019-12-03 20:25:43 -06:00
parent e5bb15e0ba
commit 4ee9e723b6
2 changed files with 29 additions and 18 deletions

View File

@@ -70,7 +70,7 @@ public:
std::unique_lock<std::mutex> lock{_mutex}; std::unique_lock<std::mutex> lock{_mutex};
if (_completed) return; if (_completed) return;
_progress = value; _progress = value;
if (static_cast<int>(_progress) == 100) { if (_progress >= 100.0) {
_completed = true; _completed = true;
} }
} }
@@ -78,8 +78,19 @@ public:
} }
void tick() { void tick() {
if (_completed) return; {
set_progress(_progress + 1); std::unique_lock<std::mutex> lock{_mutex};
if (_completed) return;
_progress += 1;
if (_progress >= 100.0) {
_completed = true;
}
}
_print_progress();
}
bool completed() {
return _completed;
} }
}; };

View File

@@ -5,7 +5,7 @@ int main() {
ProgressBar bar; ProgressBar bar;
// Configure progress bar // Configure progress bar
bar.bar_width(100); bar.bar_width(50);
bar.start_with("["); bar.start_with("[");
bar.fill_progress_with("="); bar.fill_progress_with("=");
bar.lead_progress_with(">"); bar.lead_progress_with(">");
@@ -18,24 +18,24 @@ int main() {
// //
// //
std::thread first_job( auto job = [&bar]() {
[&bar]() { while(true) {
for (size_t i = 0; i <= 50; ++i) { if (bar.completed())
bar.tick(); break;
std::this_thread::sleep_for(std::chrono::milliseconds(100)); bar.tick();
} std::this_thread::sleep_for(std::chrono::milliseconds(100));
}); }
};
std::thread second_job( std::thread first_job(job);
[&bar]() { std::thread second_job(job);
for (size_t i = 0; i <= 50; ++i) { std::thread third_job(job);
bar.tick(); std::thread last_job(job);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
});
first_job.join(); first_job.join();
second_job.join(); second_job.join();
third_job.join();
last_job.join();
return 0; return 0;
} }