Skip to content

Commit 091201b

Browse files
authored
Merge pull request #183 from babiel/fix-change-max-race
fix race condition when changing max
2 parents 37cfbad + b6dcc23 commit 091201b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

progressbar.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,17 @@ func New64(max int64) *ProgressBar {
612612

613613
// GetMax returns the max of a bar
614614
func (p *ProgressBar) GetMax() int {
615+
p.lock.Lock()
616+
defer p.lock.Unlock()
617+
615618
return int(p.config.max)
616619
}
617620

618621
// GetMax64 returns the current max
619622
func (p *ProgressBar) GetMax64() int64 {
623+
p.lock.Lock()
624+
defer p.lock.Unlock()
625+
620626
return p.config.max
621627
}
622628

@@ -632,13 +638,17 @@ func (p *ProgressBar) ChangeMax(newMax int) {
632638
// but takes in a int64
633639
// to avoid casting
634640
func (p *ProgressBar) ChangeMax64(newMax int64) {
641+
p.lock.Lock()
642+
635643
p.config.max = newMax
636644

637645
if p.config.showBytes {
638646
p.config.maxHumanized, p.config.maxHumanizedSuffix = humanizeBytes(float64(p.config.max),
639647
p.config.useIECUnits)
640648
}
641649

650+
p.lock.Unlock() // so p.Add can lock
651+
642652
p.Add(0) // re-render
643653
}
644654

0 commit comments

Comments
 (0)