Skip to content

Commit

Permalink
Merge pull request #183 from babiel/fix-change-max-race
Browse files Browse the repository at this point in the history
fix race condition when changing max
  • Loading branch information
schollz committed May 20, 2024
2 parents 37cfbad + b6dcc23 commit 091201b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,17 @@ func New64(max int64) *ProgressBar {

// GetMax returns the max of a bar
func (p *ProgressBar) GetMax() int {
p.lock.Lock()
defer p.lock.Unlock()

return int(p.config.max)
}

// GetMax64 returns the current max
func (p *ProgressBar) GetMax64() int64 {
p.lock.Lock()
defer p.lock.Unlock()

return p.config.max
}

Expand All @@ -632,13 +638,17 @@ func (p *ProgressBar) ChangeMax(newMax int) {
// but takes in a int64
// to avoid casting
func (p *ProgressBar) ChangeMax64(newMax int64) {
p.lock.Lock()

p.config.max = newMax

if p.config.showBytes {
p.config.maxHumanized, p.config.maxHumanizedSuffix = humanizeBytes(float64(p.config.max),
p.config.useIECUnits)
}

p.lock.Unlock() // so p.Add can lock

p.Add(0) // re-render
}

Expand Down

0 comments on commit 091201b

Please sign in to comment.