<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 9.4\. 錯誤的同步方式 注意:變量讀操作雖然可以偵測到變量的寫操作,但是并不能保證對變量的讀操作就一定發生在寫操作之后。 例如: ``` var a, b int func f() { a = 1; b = 2; } func g() { print(b); print(a); } func main() { go f(); g(); } ``` 函數g可能輸出2,也可能輸出0。 這種情形使得我們必須回避一些看似合理的用法。 這里用重復檢測的方法來代替同步。在例子中,twoprint函數可能得到錯誤的值: ``` var a string var done bool func setup() { a = "hello, world"; done = true; } func doprint() { if !done { once.Do(setup); } print(a); } func twoprint() { go doprint(); go doprint(); } ``` 在doprint函數中,寫done暗示已經給a賦值了。 但是沒有辦法給出保證,函數可能輸出空的值(在2個goroutines中同時執行到測試語句)。 另一個錯誤陷阱是忙等待: ``` var a string var done bool func setup() { a = "hello, world"; done = true; } func main() { go setup(); for !done { } print(a); } ``` 我們沒有辦法保證在main中看到了done值被修改的同時也 能看到a被修改,因此程序可能輸出空字符串。 更壞的結果是,main 函數可能永遠不知道done被修改,因為在兩個線程之間沒有同步操作,這樣main 函數永遠不能返回。 下面的用法本質上也是同樣的問題. ``` type T struct { msg string; } var g *T func setup() { t := new(T); t.msg = "hello, world"; g = t; } func main() { go setup(); for g == nil { } print(g.msg); } ``` 即使main觀察到了 g != nil 條件并且退出了循環,但是任何然 不能保證它看到了g.msg的初始化之后的結果。 在這些例子中,只有一種解決方法:用顯示的同步。
                  <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>

                              哎呀哎呀视频在线观看