<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # **競態(Race conditions)** `pipeline.go`并不完美,它包含一個邏輯錯誤,在并發術語中稱為**競態**。這可以通過執行以下命令來揭示: ```shell $ go run -race pipeline.go 1 10 2 2 ================== WARNING: DATA RACE Write at 0x00000122bae8 by goroutine 7: main.second() /Users/mtsouk/ch09/pipeline.go:34 +0x15c Previous read at 0x00000122bae8 by goroutine 6: main.first() /Users/mtsouk/ch09/pipeline.go:21 +0xa3 Goroutine 7 (running) created at: main.main() /Users/mtsouk/ch09/pipeline.go:72 +0x2a1 Goroutine 6 (running) created at: main.main() /Users/mtsouk/ch09/pipeline.go:71 +0x275 ================== 2 The sum of the random numbers is 2. Found 1 data race(s) exit status 66 ``` 這里的問題是,當`first()`函數讀取CLOSEA變量時,執行`second()`函數的`goroutine`可能會更改CLOSEA變量的值。因為先發生什么和后發生什么是不確定的,所以被認為是競態。為了修正這個競態條件,我們需要使用一個信號通道和`select`關鍵字。 >Tip:第十章會有更多`select`的介紹。 `diff(1)`命令會揭示新代碼`plNoRace.go`和之前代碼的區別: ```bash $ diff pipeline.go plNoRace.go 14a15,16 > var signal chan struct{} > 21c23,24 < if CLOSEA { --- > select { > case <-signal: 23a27 > case out <- random(min, max): 25d28 < out <- random(min, max) 31d33 < fmt.Print(x, " ") 34c36 < CLOSEA = true --- > signal <- struct{}{} 35a38 > fmt.Print(x, " ") 61d63 < 66a69,70 > signal = make(chan struct{}) > ``` `plNoRace.go`的正確性可以通過如下命令驗證: ```bash $ go run -race plNoRace.go 1 10 8 1 4 9 3 The sum of the random numbers is 25. ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看