Golang中重新open 已經被close的chan管道

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。最近忽發奇想,在Golang中怎麼把,已經關閉的管道再次開啟。這樣就避免了,每次都要make一個新的chan,節省記憶體申請和GC的時間,查看go的原始碼,經過簡單地摸索後,實現了,範例程式碼如下。
func TestUnsafe(t *testing.T) {c1 = make(chan struct{}, 10)c1 <- struct{}{}<-c1close(c1)p := (*unsafe.Pointer)(unsafe.Pointer(&c1))c := (*hchan)(*p)c.closed = 0//開啟被關閉的chanc1 <- struct{}{}<-c1}
func TestSafe(t *testing.T) {c1 = make(chan struct{}, 10)c1 <- struct{}{}<-c1close(c1)p := (*unsafe.Pointer)(unsafe.Pointer(&c1))c := (*hchan)(*p)atomic.CompareAndSwapUint32(&c.closed, 1, 0) //開啟被關閉的chanc1 <- struct{}{}<-c1} 

最終代碼 發布到 github.com/yireyun/go-openc,有興趣的朋友可以看看。

這種做法最大的缺點是如果go的runtime的資料結構發生變化,將不再可行。因此增加了在init方法中檢測的go核心資料結構是否發生變化的代碼,效能測試結果如下。

BenchmarkMakeCloseChan-4       20000000  90.9 ns/op--- BENCH: BenchmarkMakeCloseChan-4(每次建立在關閉的效能)go1.7.4, Times:         1, use:             0s         0s/opgo1.7.4, Times:       100, use:             0s         0s/opgo1.7.4, Times:     10000, use:        998.3µs       99ns/opgo1.7.4, Times:   1000000, use:      91.0664ms       91ns/opgo1.7.4, Times:  20000000, use:     1.8185449s       90ns/opBenchmarkCloseOpenChan-4       50000000  29.5 ns/op--- BENCH: BenchmarkCloseOpenChan-4(非安全開啟的效能)go1.7.4, Times:         1, use:             0s         0s/opgo1.7.4, Times:       100, use:             0s         0s/opgo1.7.4, Times:     10000, use:             0s         0s/opgo1.7.4, Times:   1000000, use:      28.5199ms       28ns/opgo1.7.4, Times:  50000000, use:      1.473497s       29ns/opBenchmarkCloseOpenChanSync-4   30000000  44.9 ns/op--- BENCH: BenchmarkCloseOpenChanSync-4(安全開啟的效能)go1.7.4, Times:         1, use:             0s         0s/opgo1.7.4, Times:       100, use:             0s         0s/opgo1.7.4, Times:     10000, use:        498.2µs       49ns/opgo1.7.4, Times:   1000000, use:      47.5278ms       47ns/opgo1.7.4, Times:  30000000, use:     1.3479236s       44ns/opPASSok  github.com/yireyun/go-openc8.235s
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.