Commit 8ac8e89f by Maarten L. Hekkelman

Fix progress bar by removing conditional variable

parent 2281f594
......@@ -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 stop();
void consumed(int64_t n);
void progress(int64_t p);
......@@ -164,12 +168,20 @@ struct progress_bar_impl
std::string m_action, m_message;
std::mutex m_mutex;
std::thread m_thread;
std::condition_variable m_cv;
std::chrono::time_point<std::chrono::system_clock>
m_start = std::chrono::system_clock::now();
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()
{
using namespace std::literals;
......@@ -178,18 +190,16 @@ void progress_bar_impl::run()
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)
{
std::this_thread::sleep_for(10ms);
continue;
}
std::lock_guard lock(m_mutex);
if (not printedAny and isatty(STDOUT_FILENO))
std::cout << "\e[?25l";
......@@ -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)
{
std::unique_lock lock(m_mutex);
m_consumed += n;
m_cv.notify_one();
}
void progress_bar_impl::progress(int64_t p)
{
std::unique_lock lock(m_mutex);
m_consumed = p;
m_cv.notify_one();
}
void progress_bar_impl::message(const std::string &msg)
{
std::unique_lock lock(m_mutex);
m_message = msg;
m_cv.notify_one();
}
const char* kSpinner[] = {
......@@ -374,9 +363,6 @@ progress_bar::progress_bar(int64_t inMax, const std::string &inAction)
progress_bar::~progress_bar()
{
if (m_impl != nullptr)
m_impl->stop();
delete m_impl;
}
......
......@@ -3,14 +3,14 @@
#include <random>
#include <thread>
int main()
void test_one()
{
cif::progress_bar pb(10, "test");
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(100, 1000);
cif::progress_bar pb(10, "test");
for (int i = 0; i < 10; ++i)
{
std::this_thread::sleep_for(std::chrono::milliseconds(distrib(gen)));
......@@ -18,6 +18,32 @@ int main()
pb.message("step " + std::to_string(i));
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;
}
\ 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