帶buffer的chan能同步嗎?

具體看下面2個例子:golang

無 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/YV9H6WPYuJcode

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

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

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

package main

import (
	"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上的是假時間):it

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

在本機運行的結果:test

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

能夠發現帶Buffer的chan收/發完成時間相差了10秒.import

相關文章
相關標籤/搜索