0

I've been wondering why the fmt.Print in the retiredOpCode function is not working. Though, the fmt.Print in the dispatchOpCode function working fine. The code below is the complete code (You can try to run from the go playground) and the fmt.Print in dispatchOpCode function is working.

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)
}

To be honest, i don't really know why the Print is not working. For example the print in the dispatchOpCode, it won't work if i change the Main Go Routine into this :

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

Does anyone know why it doesn't work ? or explain about this with the corrected code ? Thank you !

1 Answers1

1

You never put anything into the fromPipeline.

If I add the line fromPipeline[1] <- 2 anywhere, I'll see Complete : 2 By Pipeline : 1 printed out as retiredOpCode does its work.

LanceH
  • 1,726
  • 13
  • 20