帶buffer的chan能同步嗎?

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

具體看下面2個例子:

無 buffer 的:
http://play.golang.org/p/RwPbCcueWh

func main() {    ch := make(chan bool)    go func() {        println("fuck buffer channel")        <-ch    }()    ch <- true}

有buffer的:
http://play.golang.org/p/YV9H6WPYuJ

func main() {    ch := make(chan bool, 1)    go func() {        println("fuck buffer channel")        <-ch    }()    ch <- true}

第二個程式(有buffer的chan)無法保證收/發同步, 導致main退出時 println 還未執行.
說明帶buffer的chan是不能保證收/發同步.

下面是一個構造的測試例子
http://play.golang.org/p/vQQCv9nG15

package mainimport (    "fmt"    "sync"    "time")func main() {    testNoBufferChan()    testBufferChan()}func testNoBufferChan() {    wg := new(sync.WaitGroup)    ch := make(chan bool)    wg.Add(1)    go func() {        defer wg.Done()        time.Sleep(10 * time.Second)        fmt.Println(time.Now(), "NoBufferChan recv begin")        <-ch        fmt.Println(time.Now(), "NoBufferChan recv end")    }()    fmt.Println(time.Now(), "NoBufferChan send begin")    ch <- true    fmt.Println(time.Now(), "NoBufferChan send end")    wg.Wait()}func testBufferChan() {    wg := new(sync.WaitGroup)    ch := make(chan bool, 1)    wg.Add(1)    go func() {        defer wg.Done()        time.Sleep(10 * time.Second)        fmt.Println(time.Now(), "BufferChan recv begin")        <-ch        fmt.Println(time.Now(), "BufferChan recv end")    }()    fmt.Println(time.Now(), "BufferChan send begin")    ch <- true    fmt.Println(time.Now(), "BufferChan send end")    wg.Wait()}

在 play 上的結果(play上的是假時間):

2009-11-10 23:00:00 +0000 UTC NoBufferChan send begin2009-11-10 23:00:10 +0000 UTC NoBufferChan recv begin2009-11-10 23:00:10 +0000 UTC NoBufferChan recv end2009-11-10 23:00:10 +0000 UTC NoBufferChan send end2009-11-10 23:00:10 +0000 UTC BufferChan send begin2009-11-10 23:00:10 +0000 UTC BufferChan send end2009-11-10 23:00:20 +0000 UTC BufferChan recv begin2009-11-10 23:00:20 +0000 UTC BufferChan recv end

在本機啟動並執行結果:

2014-05-09 10:41:17.5611304 +0800 CST NoBufferChan send begin2014-05-09 10:41:27.5637025 +0800 CST NoBufferChan recv begin2014-05-09 10:41:27.5637025 +0800 CST NoBufferChan recv end2014-05-09 10:41:27.5637025 +0800 CST NoBufferChan send end2014-05-09 10:41:27.5647026 +0800 CST BufferChan send begin2014-05-09 10:41:27.5647026 +0800 CST BufferChan send end2014-05-09 10:41:37.5662746 +0800 CST BufferChan recv begin2014-05-09 10:41:37.5662746 +0800 CST BufferChan recv end

可以發現帶Buffer的chan收/發完成時間相差了10秒.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.