Go - Race

Go - Race 首页 / Golang入门教程 / Go - Race

当两个或更多goroutine尝试访问同一资源时,Go中会出现竞争条件。当变量在不考虑其他例程的情况下尝试读取和写入资源时,可能会发生这种情况。

Race 示例

package main
import (
   "sync"
   "time"
   "math/rand"
   "fmt"
)
var wait sync.WaitGroup
var count int
func  increment(s string)  {
   for i :=0;i<10;i++ {
      x := count
      x++;
      time.Sleep(time.Duration(rand.Intn(4))*time.Millisecond)
      count = x;
      fmt.Println(s, i,"Count: ",count)
      
   }
   wait.Done()
   
}
func main(){
   wait.Add(2)
   go increment("foo: ")
   go increment("bar: ")
   wait.Wait()
   fmt.Println("last count value " ,count)
}

输出:

foo:  0 Count:  1
bar:  0 Count:  1
foo:  1 Count:  2
foo:  2 Count:  3
foo:  3 Count:  4
bar:  1 Count:  2
foo:  4 Count:  5
foo:  5 Count:  6
foo:  6 Count:  7
bar:  2 Count:  3
bar:  3 Count:  4
bar:  4 Count:  5
foo:  7 Count:  8
foo:  8 Count:  9
bar:  5 Count:  6
bar:  6 Count:  7
foo:  9 Count:  10
bar:  7 Count:  8
bar:  8 Count:  9
bar:  9 Count:  10
last count value  10

如您在上面的示例中看到的,计数资源由2个go例程访问。每个例程迭代10次。在这种情况下,count变量最后应为20。并非如此,因为它是在模拟并发条件。

链接:https://www.learnfk.comhttps://www.learnfk.com/go/go-race.html

来源:LearnFk无涯教程网

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

趣谈网络协议 -〔刘超〕

技术管理实战36讲 -〔刘建国〕

许式伟的架构课 -〔许式伟〕

透视HTTP协议 -〔罗剑锋(Chrono)〕

研发效率破局之道 -〔葛俊〕

小马哥讲Spring核心编程思想 -〔小马哥〕

说透元宇宙 -〔方军〕

超级访谈:对话玉伯 -〔玉伯〕

大型Android系统重构实战 -〔黄俊彬〕

好记忆不如烂笔头。留下您的足迹吧 :)