2016-02-24 64 views
5

Aşağıdakilerin neden çalışmadığını merak ediyorum. Kanal seç <- <- Kanal

package main 

import "fmt" 

func main() { 
    a := make(chan int) 
    b := make(chan int) 

    select { 
    case a <- <- b: 
     fmt.Println("this is impossible") 
    default: 
     fmt.Println("select worked as naively expected") 
    } 
} 

Açıkçası <- <- sevmez ama burada yüzeyinin arkasında neler olduğunu merak ediyorum: Genel olarak bir default: ile select ancak bu durumda, kilitlenme önler. Diğer durumlarda <- <- izin verilir (belki de önerilmez).

cevap

6

a <- <- b için en soldaki chan ile <- operatör ilişkilendirir mümkün a<- (<-b) aynıdır.

Yani select (a<- (something) formunda) bir gönderme işlemi ile bir case sahiptir. Ve burada olan şey, gönderme ifadesinin sağ taraftaki ifadesinin (gönderilecek olan değer) ilk olarak değerlendirilmesidir - bu, <-b. (Hiç kimse b şey gönderiyor çünkü) Ama bu yüzden, sonsuza engeller:

fatal error: all goroutines are asleep - deadlock!

İlgili bölüm oluştururlar Spec: Select statements:

Execution of a "select" statement proceeds in several steps:

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

  2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.

  3. ...

Yani default varsa, select hiçbiri eğer engellenmesini önlemek yapar iletişim adım 2 içinde devam edebilir, ancak kodunuz adım 1 içinde sıkışmış. Sonra <- b değerlendirilmesi bloke olmaz b üzerinde bir değer göndereceği bir goroutine olurdu Eğer


Sadece tam olduğu, bu nedenle select yürütülmesi 2. adımda takılıp olmaz ve siz "select worked as naively expected" ( a almayı hala bu nedenle devam edemeyeceğini çünkü default seçileceğini) beklenen görecekti:

go func() { b <- 1 }() 

select { 
    // ... 
} 

o Go Playground üzerinde deneyin.

İlgili konular