去哪儿面试题 (Golang实现)多线程顺序输出1-75

FredricZhu · 2019-06-06 19:32:39

输出结果不稳定,也就是说结果不对

#2
更多评论
var a struct {

    Mu sync.RWMutex

    Count int

}


var wg sync.WaitGroup


func main()  {

    wg = sync.WaitGroup{}

    wg.Add(75)

    for i:=1; i<=75; i++ {

        go Add()

    }
    wg.Wait()
}

func Add()  {

    defer wg.Done()

    a.Mu.Lock()

    a.Count++

    fmt.Println(a.Count)

    a.Mu.Unlock()

}
#1

没有明白意思,是要控制goroutine输出顺序吗?

#3