Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
libcifpp
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
libcifpp
Commits
8ac8e89f
Unverified
Commit
8ac8e89f
authored
May 02, 2023
by
Maarten L. Hekkelman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix progress bar by removing conditional variable
parent
2281f594
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
39 deletions
+52
-39
src/utilities.cpp
+21
-35
test/spinner-test.cpp
+31
-4
No files found.
src/utilities.cpp
View file @
8ac8e89f
...
...
@@ -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
,
100
ms
);
if
(
m_stop
or
m_consumed
>=
m_max_value
)
break
;
if
(
std
::
chrono
::
system_clock
::
now
()
-
m_start
<
2
s
)
{
std
::
this_thread
::
sleep_for
(
10
ms
);
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
;
}
...
...
test/spinner-test.cpp
View file @
8ac8e89f
...
...
@@ -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
(
100
ms
);
}
int
main
()
{
test_one
();
test_two
();
test_three
();
return
0
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment