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()
}
最佳答案
我建议使用 context.WithCancel在这种情况下停止 goroutines。
关于Golang 可见性或 CPU 线程缓存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56290928/