Re: [go-nuts] Re: Wait before Add

2023-01-22 Thread Brian Candler
There is a clear race in that code. If both goroutines run simultaneously on different cores (or are context switched at just the "right" time - or just the "wrong" time, depending on how you look at it), then both will hit wg.Wait() when the waitgroup is empty, both will continue, both will ca

Re: [go-nuts] Re: Wait before Add

2023-01-22 Thread Горбешко Богдан
On 17.01.2023 10:57, Brian Candler wrote: You're supposed to do Add(1) -> Wait; and you'd normally wait once, in a single goroutine. Typical pattern: wg.Add(1) go func() {     defer wg.Done()     ... foo } wg.Add(1) go func() {     defer wg.Done()     ... bar } wg.Wait()  // wait for both gorou

[go-nuts] Re: Wait before Add

2023-01-17 Thread Brian Candler
You're supposed to do Add(1) -> Wait; and you'd normally wait once, in a single goroutine. Typical pattern: wg.Add(1) go func() { defer wg.Done() ... foo } wg.Add(1) go func() { defer wg.Done() ... bar } wg.Wait() // wait for both goroutines to complete On Tuesday, 17 January 20