Golang 可见性或 CPU 线程缓存问题

1)golang如何解决可见性问题?


2)下面的代码有什么问题吗?


package main


type Service struct {

    stop bool

}


func (s *Service) Run() {

    for !s.stop {

        //Some logic

    }

}


func (s *Service) Stop() {

    s.stop = true

}


func main() {

    s := &Service{}

    go s.Run()

    //Some logic

    s.Stop()

}


翻翻过去那场雪
浏览 106回答 1
1回答

慕斯王

在这种情况下,我建议使用context.WithCancel来停止 goroutine。
打开App,查看更多内容
随时随地看视频慕课网APP