2016-05-14 3 views
0

retiredOpCode関数のfmt.Printが機能しないのはなぜでしょうか。しかし、dispatchOpCode関数のfmt.Printはうまく動作します。 以下のコードは完全なコードです(go playgroundから実行しようとするとよい)、dispatchOpCode関数のfmt.Printが機能しています。GoLang printfはtime.Sleepを使用した後も何も表示しません。

package main 

import ("fmt" 
    "time" 
    "math/rand") 

var maxPipeline = 3  //Max Pipeline 
var maxNumber = 5  //Max Number which will be randomed 
const totalNumber = 10 //Total number will be randomed 
//======================================================================== 
// FUNCTION TO DELAY THE TIME (TO PREVENT GO FROM EXIST AFTER GO ROUTINE) 
//======================================================================== 
func delayTime(multiplier time.Duration){ 
    time.Sleep(time.Millisecond * multiplier) 
} 
//======================================================================== 
//       GENERATE THE OPCODES 
//======================================================================== 
func generateOpCode(opCode chan int){ 
    opCode <- rand.Intn(maxNumber) + 1 //Generate the random number with consistent sequence 
} 
//======================================================================== 
//       DISPATCH THE OPCODE 
//======================================================================== 
func dispatchOpCode(opCode chan int, toPipeline []chan int, nextPipeline []chan bool){ 
    tempPipeline := 0 //Temporary for choosing which pipeline 
    select{  //Assign the OP Code to the ready pipeline, and pass the OP Code to the selected pipeline 
     case <- nextPipeline[0] : toPipeline[0] = opCode ; tempPipeline = 0 
     case <- nextPipeline[1] : toPipeline[1] = opCode ; tempPipeline = 1 
     case <- nextPipeline[2] : toPipeline[2] = opCode ; tempPipeline = 2 
    } 
    fmt.Println("Op Code :",<-opCode," To Pipeline :",tempPipeline) 
} 
//======================================================================== 
//    PROCESS THE PIPELINE TO RETIRE FUNCTION 
//======================================================================== 
func pipelineProcess(fromDispatcher chan int, retireOpCode chan int,nextPipeline chan bool){ 
    retireOpCode = fromDispatcher //Pass the opCode to the retire Op Code 
    nextPipeline <- true   //Tell the pipeline used it's finished 
} 
//======================================================================== 
//       RETIRE THE OP CODE 
//======================================================================== 
func retiredOpCode(fromPipeline []chan int){ 
    tempPipeline := 0 //Temporary for choosing which pipeline used 
    select{  //Read the retired code from selected pipeline and assign the used pipeline 
     case opCodes :=<- fromPipeline[0] : tempPipeline = 0 ; fmt.Println("Complete :",opCodes," By Pipeline :",tempPipeline) 
     case opCodes :=<- fromPipeline[1] : tempPipeline = 1 ; fmt.Println("Complete :",opCodes," By Pipeline :",tempPipeline) 
     case opCodes :=<- fromPipeline[2] : tempPipeline = 2 ; fmt.Println("Complete :",opCodes," By Pipeline :",tempPipeline) 
    } 

} 
//======================================================================== 
//        MAIN FUNCTION 
//======================================================================== 
func main(){ 
    //=============== Create Channels =============== 
    opCode := make(chan int,1) 
    toPipeline := make([]chan int, maxPipeline) 
    for i:= range toPipeline{ 
     toPipeline[i] = make(chan int) 
    } 
    fromPipeline := make([]chan int, maxPipeline) 
    for i:= range fromPipeline{ 
     fromPipeline[i] = make(chan int) 
    } 
    nextPipeline := make([]chan bool, maxPipeline) 
    for i:= range nextPipeline{ 
     nextPipeline[i] = make(chan bool) 
    } 
    //=============== Main Go Routine =============== 
    for j:=0;j<totalNumber;j++{ 
     go generateOpCode(opCode) 
     go dispatchOpCode(opCode, toPipeline, nextPipeline) 
     for i:=0;i<maxPipeline;i++{ 
      go pipelineProcess(toPipeline[i],fromPipeline[i],nextPipeline[i]) 
     } 
     go retiredOpCode(fromPipeline) 
     go fmt.Println("=========================================") 
    } 
    delayTime(1000) 
} 

実際、なぜプリントが機能していないのかわかりません。私はこのにメインゴールーチンを変更した場合たとえば、dispatchOpCodeの印刷は、それが動作しません:それは動作しない理由を

for j:=0;j<totalNumber;j++{ 
     go generateOpCode(opCode) 
     go dispatchOpCode(opCode, toPipeline, nextPipeline) 
     go fmt.Println("=========================================") 
    } 

誰でも知っていますか?修正されたコードでこれについて説明してください。ありがとうございました !

+0

Javaでコードを作成しましたか?あなたのコードは何らかの理由でGoをJavaのように見せます。 – PieOhPah

+0

ステージとチャンネルの間でよりクリーンな同期を作成するには[Go pipeline pattern](https://blog.golang.org/pipelines)のビットを読んでみることをお勧めします。 – PieOhPah

+0

@PieOhPah、アドバイスありがとうございました! –

答えて

1

fromPipelineには何も入れません。

fromPipeline[1] <- 2の行をどこに追加しても、が表示され、retiredOpCodeと表示されます。

+0

あなたはfromPipeline [1] < - 2をどこに配置しましたか?私は 'retiredOpCode'に入れてみましたが、それでも動作しません。 –

+0

私はfmt.Println()の後に、main()のforループの最後に置いておきます。 – LanceH

関連する問題