最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Go select 死鎖的一個細節(jié)

 更新時間:2021年10月08日 15:02:16   作者:polarisxu  
這篇文章主要給大家分享的是Go select 死鎖的一個細節(jié),文章先是對主題提出問題,然后展開內(nèi)容,感興趣的小伙伴可以借鑒一下,希望對你有所幫助

下面對是一個 select 死鎖的問題

package main

import "sync"

func main() {
 var wg sync.WaitGroup
 foo := make(chan int)
 bar := make(chan int)
 wg.Add(1)
 go func() {
  defer wg.Done()
  select {
  case foo <- <-bar:
  default:
   println("default")
  }
 }()
 wg.Wait()
}

按常規(guī)理解,go func 中的 select 應(yīng)該執(zhí)行 default 分支,程序正常運行。但結(jié)果卻不是,而是死鎖??梢酝ㄟ^該鏈接測試:https://play.studygolang.com/p/kF4pOjYXbXf。

原因文章也解釋了,Go 語言規(guī)范中有這么一句:

For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the “select” statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.

不知道大家看懂沒有?于是,最后來了一個例子驗證你是否理解了:為什么每次都是輸出一半數(shù)據(jù),然后死鎖?(同樣,這里可以運行查看結(jié)果:https://play.studygolang.com/p/zoJtTzI7K5T)

package main

import (
 "fmt"
 "time"
)

func talk(msg string, sleep int) <-chan string {
 ch := make(chan string)
 go func() {
  for i := 0; i < 5; i++ {
   ch <- fmt.Sprintf("%s %d", msg, i)
   time.Sleep(time.Duration(sleep) * time.Millisecond)
  }
 }()
 return ch
}

func fanIn(input1, input2 <-chan string) <-chan string {
 ch := make(chan string)
 go func() {
  for {
   select {
   case ch <- <-input1:
   case ch <- <-input2:
   }
  }
 }()
 return ch
}

func main() {
 ch := fanIn(talk("A", 10), talk("B", 1000))
 for i := 0; i < 10; i++ {
  fmt.Printf("%q\n", <-ch)
 }
}

有沒有這種感覺:

這是 StackOverflow 上的一個問題:https://stackoverflow.com/questions/51167940/chained-channel-operations-in-a-single-select-case。

關(guān)鍵點和文章開頭例子一樣,在于 select case 中兩個 channel 串起來,即 fanIn 函數(shù)中:

select {
case ch <- <-input1:
case ch <- <-input2:
}


如果改為這樣就一切正常:

select {
case t := <-input1:
  ch <- t
case t := <-input2:
  ch <- t
}

結(jié)合這個更復(fù)雜的例子分析 Go 語言規(guī)范中的那句話。

對于 select 語句,在進入該語句時,會按源碼的順序?qū)γ恳粋€ case 子句進行求值:這個求值只針對發(fā)送或接收操作的額外表達式。

比如:

// ch 是一個 chan int;
// getVal() 返回 int
// input 是 chan int
// getch() 返回 chan int
select {
  case ch <- getVal():
  case ch <- <-input:
  case getch() <- 1:
  case <- getch():
}

在沒有選擇某個具體 case 執(zhí)行前,例子中的 getVal() <-input getch() 會執(zhí)行。這里有一個驗證的例子:https://play.studygolang.com/p/DkpCq3aQ1TE。

package main

import (
 "fmt"
)

func main() {
 ch := make(chan int)
 go func() {
  select {
  case ch <- getVal(1):
   fmt.Println("in first case")
  case ch <- getVal(2):
   fmt.Println("in second case")
  default:
   fmt.Println("default")
  }
 }()

 fmt.Println("The val:", <-ch)
}

func getVal(i int) int {
 fmt.Println("getVal, i=", i)
 return i
}

無論 select 最終選擇了哪個 casegetVal() 都會按照源碼順序執(zhí)行: getVal(1) getVal(2) ,也就是它們必然先輸出:

getVal, i= 1
getVal, i= 2

你可以仔細琢磨一下。

現(xiàn)在回到 StackOverflow 上的那個問題。

每次進入以下 select 語句時:

select {
case ch <- <-input1:
case ch <- <-input2:
}


<-input1 和 <-input2 都會執(zhí)行,相應(yīng)的值是:A x 和 B x(其中 x 是 0-5)。但每次 select 只會選擇其中一個 case 執(zhí)行,所以 <-input1 和 <-input2 的結(jié)果,必然有一個被丟棄了,也就是不會被寫入 ch 中。因此,一共只會輸出 5 次,另外 5 次結(jié)果丟掉了。(你會發(fā)現(xiàn),輸出的 5 次結(jié)果中,x 比如是 0 1 2 3 4)

main 中循環(huán) 10 次,只獲得 5 次結(jié)果,所以輸出 5 次后,報死鎖。

雖然這是一個小細節(jié),但實際開發(fā)中還是有可能出現(xiàn)的。比如文章提到的例子寫法:

// ch 是一個 chan int;
// getVal() 返回 int
// input 是 chan int
// getch() 返回 chan int
select {
  case ch <- getVal():
  case ch <- <-input:
  case getch() <- 1:
  case <- getch():
}

因此在使用 select 時,一定要注意這種可能的問題。

不要以為這個問題不會遇到,其實很常見。最多的就是 time.After 導(dǎo)致內(nèi)存泄露問題,網(wǎng)上有很多文章解釋原因,如何避免,其實最根本原因就是因為 select 這個機制導(dǎo)致的。

比如如下代碼,有內(nèi)存泄露(傳遞給 time.After 的時間參數(shù)越大,泄露會越厲害),你能解釋原因嗎?

package main

import (
    "time"
)

func main()  {
    ch := make(chan int, 10)

    go func() {
        var i = 1
        for {
            i++
            ch <- i
        }
    }()

    for {
        select {
        case x := <- ch:
            println(x)
        case <- time.After(30 * time.Second):
            println(time.Now().Unix())
        }
    }
}

到此這篇關(guān)于Go select 死鎖的一個細節(jié)的文章就介紹到這了,更多相關(guān)Go select 死鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go中的go.mod使用詳解

    Go中的go.mod使用詳解

    這篇文章主要介紹了Go中的go.mod使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Go語言學(xué)習(xí)教程之goroutine和通道的示例詳解

    Go語言學(xué)習(xí)教程之goroutine和通道的示例詳解

    這篇文章主要通過A?Tour?of?Go中的例子進行學(xué)習(xí),以此了解Go語言中的goroutine和通道,文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-09-09
  • GO語言實現(xiàn)簡單TCP服務(wù)的方法

    GO語言實現(xiàn)簡單TCP服務(wù)的方法

    這篇文章主要介紹了GO語言實現(xiàn)簡單TCP服務(wù)的方法,實例分析了Go語言實現(xiàn)TCP服務(wù)的技巧,需要的朋友可以參考下
    2015-03-03
  • Golang實現(xiàn)IO操作

    Golang實現(xiàn)IO操作

    本文主要介紹了Golang實現(xiàn)IO操作,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Go語言數(shù)據(jù)結(jié)構(gòu)之二叉樹必會知識點總結(jié)

    Go語言數(shù)據(jù)結(jié)構(gòu)之二叉樹必會知識點總結(jié)

    如果你是一個開發(fā)人員,或多或少對樹型結(jié)構(gòu)都有一定的認識。二叉樹作為樹的一種,是一種重要的數(shù)據(jù)結(jié)構(gòu),也是面試官經(jīng)??嫉臇|西。本文為大家總結(jié)了一些二叉樹必會知識點,需要的可以參考一下
    2022-08-08
  • go使用Gin框架利用阿里云實現(xiàn)短信驗證碼功能

    go使用Gin框架利用阿里云實現(xiàn)短信驗證碼功能

    這篇文章主要介紹了go使用Gin框架利用阿里云實現(xiàn)短信驗證碼,使用json配置文件及配置文件解析,編寫路由controller層,本文通過代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2021-08-08
  • golang反向代理設(shè)置host不生效的問題解決

    golang反向代理設(shè)置host不生效的問題解決

    在使用golang的httputil做反向代理的時候,發(fā)現(xiàn)一個奇怪的現(xiàn)象,上游網(wǎng)關(guān)必須要設(shè)置host才行,不設(shè)置host的話,golang服務(wù)反向代理請求下游會出現(xiàn)http 503錯誤,接下來通過本文給大家介紹golang反向代理設(shè)置host不生效問題,感興趣的朋友一起看看吧
    2023-05-05
  • 初步解讀Golang中的接口相關(guān)編寫方法

    初步解讀Golang中的接口相關(guān)編寫方法

    這篇文章主要介紹了Golang中的接口相關(guān)編寫方法,是Go語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11
  • Go?Redis客戶端使用的兩種對比

    Go?Redis客戶端使用的兩種對比

    這篇文章主要為大家介紹了Go?Redis客戶端使用對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • go語言編程之美自定義二進制文件實用指南

    go語言編程之美自定義二進制文件實用指南

    這篇文章主要介紹了go語言編程之美自定義二進制文件實用指南
    2023-12-12

最新評論

洛南县| 鸡西市| 定远县| 临潭县| 汕头市| 堆龙德庆县| 宜黄县| 林芝县| 孟连| 屯留县| 庆元县| 石首市| 北流市| 南阳市| 晋江市| 宜昌市| 图们市| 玉环县| 武清区| 德令哈市| 乌海市| 广平县| 聂荣县| 丰城市| 乳山市| 江阴市| 酒泉市| 炉霍县| 黄山市| 无为县| 舟曲县| 临颍县| 栾城县| 常德市| 鄂托克旗| 河间市| 孝昌县| 道真| 重庆市| 长春市| 合山市|