Skip to content

Commit eb87c55

Browse files
committed
relaxed memory order
1 parent 4f07db0 commit eb87c55

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

progress_bar.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ProgressBar::ProgressBar(uint64_t total,
5656

5757
description_.resize(kMessageSize, ' ');
5858

59-
ShowProgress();
59+
ShowProgress(0);
6060
}
6161

6262
ProgressBar::~ProgressBar() {
@@ -120,14 +120,14 @@ std::string get_progress_summary(double progress_ratio) {
120120
return buffer;
121121
}
122122

123-
void ProgressBar::ShowProgress() const {
123+
void ProgressBar::ShowProgress(uint64_t progress) const {
124124
if (silent_)
125125
return;
126126

127127
std::lock_guard<std::mutex> lock(mu_);
128128

129129
// calculate percentage of progress
130-
double progress_ratio = total_ ? static_cast<double>(progress_) / total_
130+
double progress_ratio = total_ ? static_cast<double>(progress) / total_
131131
: 1.0;
132132
assert(progress_ratio >= 0.0);
133133
assert(progress_ratio <= 1.0);
@@ -142,7 +142,7 @@ void ProgressBar::ShowProgress() const {
142142
os << std::put_time(std::localtime(&time), "[%F %T.")
143143
<< std::setfill('0') << std::setw(3) << ms.count() << "]\t"
144144
<< 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';
146146
*out << os.str() << std::flush;
147147
return;
148148
}
@@ -163,7 +163,7 @@ void ProgressBar::ShowProgress() const {
163163
+ std::string(size_t(bar_size * progress_ratio), unit_bar_)
164164
+ std::string(bar_size - size_t(bar_size * progress_ratio), unit_space_)
165165
+ "] " + 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';
167167

168168
*out << buffer_ << std::flush;
169169

@@ -182,15 +182,16 @@ ProgressBar& ProgressBar::operator+=(uint64_t delta) {
182182
if (silent_ || !delta)
183183
return *this;
184184

185-
uint64_t after_update = (progress_ += delta);
185+
uint64_t after_update
186+
= progress_.fetch_add(delta, std::memory_order_relaxed) + delta;
186187

187188
assert(after_update <= total_);
188189

189190
// determines whether to update the progress bar from frequency_update
190191
if (after_update == total_
191192
|| (after_update - delta) / frequency_update
192193
< after_update / frequency_update)
193-
ShowProgress();
194+
ShowProgress(after_update);
194195

195196
return *this;
196197
}

progress_bar.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ProgressBar {
3232
ProgressBar(const ProgressBar &) = delete;
3333
ProgressBar& operator=(const ProgressBar &) = delete;
3434

35-
void ShowProgress() const;
35+
void ShowProgress(uint64_t progress) const;
3636
int GetConsoleWidth() const;
3737
int GetBarLength() const;
3838

0 commit comments

Comments
 (0)