比赛条件改变

时间:2018-08-04 06:59:08

标签: go

《 Go in action》关于种族状况的样本:

var (
    counter int
    wg sync.WaitGroup
)

func main() {
    wg.Add(2)
    go incCounter(1)
    go incCounter(2)

    wg.Wait()
    fmt.Println("Final Counter:", counter)
}

func incCounter(id int) {
    defer wg.Done()

    for count := 0; count < 2; count++ {
        value := counter
        //1 fmt.Println("value=",value)
        runtime.Gosched()

        value++

        counter = value
        //2 fmt.Println("counter=",counter)
    }
}

据说最终计数器应为2,在此说明: “每个goroutine都会覆盖另一个goroutine的工作。 交换正在进行中。每个goroutine都会自己制作计数器变量的副本, 然后换成另一个goroutine。给goroutine有时间再次执行时,counter变量的值已更改,但goroutine没有 更新其副本。而是继续增加其副本并设置值 返回计数器变量,替换其他goroutine执行的工作。”

我想这是环境原因,我的机器输出4到1.10.3 + win10。我想知道自本书发行以来发生了什么变化?如果我取消注释1,则最终计数器将打印2;如果我取消注释2,则将随机打印2。为什么?

1 个答案:

答案 0 :(得分:5)

这本书是错误的。关于数据争用的要点是结果不确定。

例如,Final Counter可以是任何值。

package main

import (
    "fmt"
    "runtime"
    "sync"
)

var (
    counter int
    wg      sync.WaitGroup
)

func main() {
    wg.Add(2)
    go incCounter(1)
    go incCounter(2)

    wg.Wait()
    fmt.Println("Final Counter:", counter)
}

func incCounter(id int) {
    defer wg.Done()

    for count := 0; count < 2; count++ {
        value := counter
        //1 fmt.Println("value=",value)
        runtime.Gosched()

        value++

        counter = value
        //2 fmt.Println("counter=",counter)
    }
}

输出:

$ go version
go version devel +65fa2b615b Fri Aug 3 23:35:53 2018 +0000 linux/amd64
$ go run racer.go
Final Counter: 4
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 4
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 4
$ go run -race racer.go
==================
WARNING: DATA RACE
Read at 0x0000005e4600 by goroutine 7:
  main.incCounter()
      /home/peter/gopath/src/racer.go:27 +0x6f

Previous write at 0x0000005e4600 by goroutine 6:
  main.incCounter()
      /home/peter/gopath/src/racer.go:33 +0x90

Goroutine 7 (running) created at:
  main.main()
      /home/peter/gopath/src/racer.go:17 +0x89

Goroutine 6 (finished) created at:
  main.main()
      /home/peter/gopath/src/racer.go:16 +0x68
==================
Final Counter: 4
Found 1 data race(s)
exit status 66
$