9

I'm new to Go and have a problem understanding the concurrency and channel.

package main

import "fmt"

func display(msg string, c chan bool){
    fmt.Println("display first message:", msg)
    c <- true
}

func sum(c chan bool){
    sum := 0
    for i:=0; i < 10000000000; i++ {
        sum++
    }
    fmt.Println(sum)
    c <- true
}

func main(){
    c := make(chan bool)

    go display("hello", c)
    go sum(c)
    <-c
}

The output of the program is:

display first message: hello
10000000000 

But I thought it should be only one line:

display first message: hello

So in the main function, <-c is blocking it and waits for the other two go rountines to send data to the channel. Once the main function receives the data from c, it should proceed and exit.

display and sum run simultaneously and sum takes longer so display should send true to c and the program should exit before sum finishes...

I'm not sure I understand it clearly. Could someone help me with this? Thank you!

1
  • As tux21b suggests, it's probably because of runtime.GOMAXPROCS. Bump it up, and you may see a difference.
    – dyoo
    Aug 23, 2013 at 1:10

1 Answer 1

4

The exact output of your program is not defined and depends on the scheduler. The scheduler can choose freely between all goroutines that are currently not blocked. It tries to run those goroutines concurrently by switching the current goroutine in very short time intervals so that the user gets the feeling that everything happens simultanously. In addition to that, it can also execute more than one goroutine in parallel on different CPUs (if you happen to have a multicore system and increase runtime.GOMAXPROCS). One situation that might lead to your output is:

  1. main creates two goroutines
  2. the scheduler chooses to switch to one of the new goroutines immediately and chooses display
  3. display prints out the message and is blocked by the channel send (c <- true) since there isn't a receiver yet.
  4. the scheduler chooses to run sum next
  5. the sum is computed and printed on the screen
  6. the scheduler chooses to not resume the sum goroutine (it has already used a fair amount of time) and continues with display
  7. display sends the value to the channel
  8. the scheduler chooses to run main next
  9. main quits and all goroutines are destroyed

But that is just one possible execution order. There are many others and some of them will lead to a different output. If you want to print just the first result and quit the program afterwards, you should probably use a result chan string and change your main function to print fmt.Println(<-result).

3
  • Thanks for the clarification. In step 3, display is blocked by the channel send since there isn't a receiver. Eh...When is the receiver ready?
    – user959704
    Aug 23, 2013 at 1:51
  • 1
    The channel is synchronous which means that both, the receiver and the sender must be ready in order to transmit the value. If a goroutine executes c <- true first, it will be blocked until another goroutine executes <-c. But it is also possible that a goroutine that executes <-c is blocked until there is a matching c <- true in another goroutine.
    – tux21b
    Aug 23, 2013 at 10:12
  • Just a comment. As far as I know, the Go scheduler in current version is not preemptive. Once a goroutine has the CPU, it won't switch to another one until it is blocked (due to I/O, channel, mutex, etc.). So if sum is executed first, it will use the CPU exclusively until blocked by the channel (I'm not sure if fmt.Println may produce a CPU switch). A preemptive scheduler is planned for Go 1.2, I read. Of course, if GOMAXPROCS is not 1, the other goroutine will use another SO-level process and will be scheduled by the SO. More info here: dominik.honnef.co/go-tip/2013-08-15
    – siritinga
    Aug 27, 2013 at 13:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.