Created
October 23, 2017 09:49
-
-
Save shelomentsevd/8d84bf38dfb44aa029d4b272cbb3c693 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "sync/atomic" | |
| "fmt" | |
| ) | |
| type Increment struct { | |
| count uint32 | |
| } | |
| func main() { | |
| increment := Increment{} | |
| start := make(chan struct{}) | |
| activity := func(i *Increment) { | |
| <-start | |
| atomic.AddUint32(&i.count, 1) | |
| } | |
| for i := 0; i < 100; i++ { | |
| go activity(&increment) | |
| } | |
| close(start) | |
| fmt.Printf("INCR: %d", increment.count) | |
| } |
Author
package main
import (
"sync/atomic"
"fmt"
"sync"
)
type Increment struct {
count uint32
}
func main() {
wg := sync.WaitGroup{}
increment := Increment{}
start := make(chan struct{})
activity := func(i *Increment) {
<-start
atomic.AddUint32(&i.count, 1)
wg.Done()
}
for i := 0; i < 10000; i++ {
wg.Add(1)
go activity(&increment)
}
close(start)
wg.Wait()
fmt.Printf("INCR: %d", increment.count)
}
Так не лучше?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Забыл добавить WaitGroup