@@ -56,7 +56,7 @@ ProgressBar::ProgressBar(uint64_t total,
56
56
57
57
description_.resize (kMessageSize , ' ' );
58
58
59
- ShowProgress ();
59
+ ShowProgress (0 );
60
60
}
61
61
62
62
ProgressBar::~ProgressBar () {
@@ -120,14 +120,14 @@ std::string get_progress_summary(double progress_ratio) {
120
120
return buffer;
121
121
}
122
122
123
- void ProgressBar::ShowProgress () const {
123
+ void ProgressBar::ShowProgress (uint64_t progress ) const {
124
124
if (silent_)
125
125
return ;
126
126
127
127
std::lock_guard<std::mutex> lock (mu_);
128
128
129
129
// calculate percentage of progress
130
- double progress_ratio = total_ ? static_cast <double >(progress_ ) / total_
130
+ double progress_ratio = total_ ? static_cast <double >(progress ) / total_
131
131
: 1.0 ;
132
132
assert (progress_ratio >= 0.0 );
133
133
assert (progress_ratio <= 1.0 );
@@ -142,7 +142,7 @@ void ProgressBar::ShowProgress() const {
142
142
os << std::put_time (std::localtime (&time), " [%F %T." )
143
143
<< std::setfill (' 0' ) << std::setw (3 ) << ms.count () << " ]\t "
144
144
<< get_progress_summary (progress_ratio)
145
- << " , " + std::to_string (progress_ ) + " /" + std::to_string (total_) + ' \n ' ;
145
+ << " , " + std::to_string (progress ) + " /" + std::to_string (total_) + ' \n ' ;
146
146
*out << os.str () << std::flush;
147
147
return ;
148
148
}
@@ -163,7 +163,7 @@ void ProgressBar::ShowProgress() const {
163
163
+ std::string (size_t (bar_size * progress_ratio), unit_bar_)
164
164
+ std::string (bar_size - size_t (bar_size * progress_ratio), unit_space_)
165
165
+ " ] " + get_progress_summary (progress_ratio)
166
- + " , " + std::to_string (progress_ ) + " /" + std::to_string (total_) + ' \r ' ;
166
+ + " , " + std::to_string (progress ) + " /" + std::to_string (total_) + ' \r ' ;
167
167
168
168
*out << buffer_ << std::flush;
169
169
@@ -182,15 +182,16 @@ ProgressBar& ProgressBar::operator+=(uint64_t delta) {
182
182
if (silent_ || !delta)
183
183
return *this ;
184
184
185
- uint64_t after_update = (progress_ += delta);
185
+ uint64_t after_update
186
+ = progress_.fetch_add (delta, std::memory_order_relaxed) + delta;
186
187
187
188
assert (after_update <= total_);
188
189
189
190
// determines whether to update the progress bar from frequency_update
190
191
if (after_update == total_
191
192
|| (after_update - delta) / frequency_update
192
193
< after_update / frequency_update)
193
- ShowProgress ();
194
+ ShowProgress (after_update );
194
195
195
196
return *this ;
196
197
}
0 commit comments