Commit 8ac8e89f by Maarten L. Hekkelman

Fix progress bar by removing conditional variable

parent 2281f594
...@@ -147,8 +147,12 @@ struct progress_bar_impl ...@@ -147,8 +147,12 @@ struct progress_bar_impl
{ {
} }
progress_bar_impl(const progress_bar_impl&) = delete;
progress_bar_impl &operator=(const progress_bar_impl &) = delete;
~progress_bar_impl();
void run(); void run();
void stop();
void consumed(int64_t n); void consumed(int64_t n);
void progress(int64_t p); void progress(int64_t p);
...@@ -164,12 +168,20 @@ struct progress_bar_impl ...@@ -164,12 +168,20 @@ struct progress_bar_impl
std::string m_action, m_message; std::string m_action, m_message;
std::mutex m_mutex; std::mutex m_mutex;
std::thread m_thread; std::thread m_thread;
std::condition_variable m_cv;
std::chrono::time_point<std::chrono::system_clock> std::chrono::time_point<std::chrono::system_clock>
m_start = std::chrono::system_clock::now(); m_start = std::chrono::system_clock::now();
bool m_stop = false; bool m_stop = false;
}; };
progress_bar_impl::~progress_bar_impl()
{
using namespace std::literals;
assert(m_thread.joinable());
m_stop = true;
m_thread.join();
}
void progress_bar_impl::run() void progress_bar_impl::run()
{ {
using namespace std::literals; using namespace std::literals;
...@@ -178,18 +190,16 @@ void progress_bar_impl::run() ...@@ -178,18 +190,16 @@ void progress_bar_impl::run()
try try
{ {
for (;;) while (not m_stop)
{ {
std::unique_lock lock(m_mutex);
m_cv.wait_for(lock, 100ms);
if (m_stop or m_consumed >= m_max_value)
break;
if (std::chrono::system_clock::now() - m_start < 2s) if (std::chrono::system_clock::now() - m_start < 2s)
{
std::this_thread::sleep_for(10ms);
continue; continue;
}
std::lock_guard lock(m_mutex);
if (not printedAny and isatty(STDOUT_FILENO)) if (not printedAny and isatty(STDOUT_FILENO))
std::cout << "\e[?25l"; std::cout << "\e[?25l";
...@@ -210,41 +220,20 @@ void progress_bar_impl::run() ...@@ -210,41 +220,20 @@ void progress_bar_impl::run()
} }
} }
void progress_bar_impl::stop()
{
{
std::unique_lock lock(m_mutex);
m_stop = true;
m_cv.notify_one();
}
if (m_thread.joinable())
m_thread.join();
}
void progress_bar_impl::consumed(int64_t n) void progress_bar_impl::consumed(int64_t n)
{ {
std::unique_lock lock(m_mutex);
m_consumed += n; m_consumed += n;
m_cv.notify_one();
} }
void progress_bar_impl::progress(int64_t p) void progress_bar_impl::progress(int64_t p)
{ {
std::unique_lock lock(m_mutex);
m_consumed = p; m_consumed = p;
m_cv.notify_one();
} }
void progress_bar_impl::message(const std::string &msg) void progress_bar_impl::message(const std::string &msg)
{ {
std::unique_lock lock(m_mutex); std::unique_lock lock(m_mutex);
m_message = msg; m_message = msg;
m_cv.notify_one();
} }
const char* kSpinner[] = { const char* kSpinner[] = {
...@@ -374,9 +363,6 @@ progress_bar::progress_bar(int64_t inMax, const std::string &inAction) ...@@ -374,9 +363,6 @@ progress_bar::progress_bar(int64_t inMax, const std::string &inAction)
progress_bar::~progress_bar() progress_bar::~progress_bar()
{ {
if (m_impl != nullptr)
m_impl->stop();
delete m_impl; delete m_impl;
} }
......
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
#include <random> #include <random>
#include <thread> #include <thread>
int main() void test_one()
{ {
cif::progress_bar pb(10, "test");
std::random_device rd; std::random_device rd;
std::mt19937 gen(rd()); std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(100, 1000); std::uniform_int_distribution<> distrib(100, 1000);
cif::progress_bar pb(10, "test");
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(distrib(gen))); std::this_thread::sleep_for(std::chrono::milliseconds(distrib(gen)));
...@@ -18,6 +18,32 @@ int main() ...@@ -18,6 +18,32 @@ int main()
pb.message("step " + std::to_string(i)); pb.message("step " + std::to_string(i));
pb.consumed(1); pb.consumed(1);
} }
}
void test_two()
{
cif::progress_bar pb(10, "test");
for (int i = 0; i < 5; ++i)
pb.consumed(1);
}
void test_three()
{
using namespace std::literals;
cif::progress_bar pb(10, "test");
pb.consumed(10);
std::this_thread::sleep_for(100ms);
}
int main()
{
test_one();
test_two();
test_three();
return 0; return 0;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment