35 lines
387 B
Go
35 lines
387 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Sth struct {
|
|
x int64
|
|
wg sync.WaitGroup
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func (s *Sth) add() {
|
|
for i := 0; i < 10000; i++ {
|
|
s.lock.Lock()
|
|
s.x++
|
|
s.lock.Unlock()
|
|
}
|
|
s.wg.Done()
|
|
}
|
|
|
|
func main() {
|
|
var s Sth
|
|
s.wg.Add(2)
|
|
go s.add()
|
|
go s.add()
|
|
s.wg.Wait()
|
|
fmt.Println(s)
|
|
|
|
time.Sleep(time.Second)
|
|
fmt.Println("xiaowei zhaodandan")
|
|
}
|