refactoring barExtenderRev
minor changes and some comment
Vladimir Bauer
3 years ago
| 42 | 42 | |
| 43 | 43 | for i := 0; i < numTasks; i++ { |
| 44 | 44 | bar := p.AddBar(tasks[i].total, |
| 45 | mpb.BarExtender(filler, true), | |
| 45 | mpb.BarExtender(filler, true), // all bars share same extender filler | |
| 46 | 46 | mpb.BarFuncOptional(func() mpb.BarOption { |
| 47 | 47 | return mpb.BarQueueAfter(tasks[i-1].bar) |
| 48 | 48 | }, i != 0), |
| 71 | 71 | |
| 72 | 72 | for _, t := range tasks { |
| 73 | 73 | atomic.StoreUint32(&curTask, t.id) |
| 74 | complete(tb, t) | |
| 74 | complete(t.bar, tb) | |
| 75 | 75 | atomic.AddUint32(&doneTasks, 1) |
| 76 | 76 | } |
| 77 | 77 | |
| 119 | 119 | }) |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | func complete(tb *mpb.Bar, t *task) { | |
| 123 | bar := t.bar | |
| 122 | func complete(bar, totalBar *mpb.Bar) { | |
| 124 | 123 | max := 100 * time.Millisecond |
| 125 | 124 | for !bar.Completed() { |
| 126 | 125 | n := rand.Int63n(10) + 1 |
| 127 | bar.IncrInt64(n) | |
| 128 | go tb.IncrInt64(n) | |
| 126 | incrementBars(n, bar, totalBar) | |
| 129 | 127 | time.Sleep(time.Duration(n) * max / 10) |
| 130 | 128 | } |
| 131 | 129 | bar.Wait() |
| 132 | 130 | } |
| 131 | ||
| 132 | func incrementBars(n int64, bb ...*mpb.Bar) { | |
| 133 | for _, b := range bb { | |
| 134 | b.IncrInt64(n) | |
| 135 | } | |
| 136 | } | |