Concurrency

It is said that Go is the C of the 21st century. I think there are two reasons for it. First, Go is a simple language. Second, concurrency is a hot topic in today’s world, and Go supports this feature at the language level.

goroutine

goroutines and concurrency are built into the core design of Go. They’re similar to threads but work differently. Go also gives you full support to sharing memory in your goroutines. One goroutine usually uses 4~5 KB of stack memory. Therefore, it’s not hard to run thousands of goroutines on a single computer. A goroutine is more lightweight, more efficient and more convenient than system threads.

goroutines run on the thread manager at runtime in Go. We use the go keyword to create a new goroutine, which is a function at the underlying level ( main() is a goroutine ).

  1. go hello(a, b, c)

Let’s see an example.

  1. package main
  2. import (
  3. "fmt"
  4. "runtime"
  5. )
  6. func say(s string) {
  7. for i := 0; i < 5; i++ {
  8. runtime.Gosched()
  9. fmt.Println(s)
  10. }
  11. }
  12. func main() {
  13. go say("world") // create a new goroutine
  14. say("hello") // current goroutine
  15. }

Output:

  1. hello
  2. world
  3. hello
  4. world
  5. hello
  6. world
  7. hello
  8. world
  9. hello

We see that it’s very easy to use concurrency in Go by using the keyword go. In the above example, these two goroutines share some memory, but we would better off following the design recipe: Don’t use shared data to communicate, use communication to share data.

runtime.Gosched() means let the CPU execute other goroutines, and come back at some point.

In Go 1.5,the runtime now sets the default number of threads to run simultaneously, defined by GOMAXPROCS, to the number of cores available on the CPU.

Before Go 1.5,The scheduler only uses one thread to run all goroutines, which means it only implements concurrency. If you want to use more CPU cores in order to take advantage of parallel processing, you have to call runtime.GOMAXPROCS(n) to set the number of cores you want to use. If n<1, it changes nothing.

channels

goroutines run in the same memory address space, so you have to maintain synchronization when you want to access shared memory. How do you communicate between different goroutines? Go uses a very good communication mechanism called channel. A channel is like two-way pipeline in Unix shells: use channel to send or receive data. The only data type that can be used in channels is the type channel and the keyword chan. Be aware that you have to use make to create a new channel.

  1. ci := make(chan int)
  2. cs := make(chan string)
  3. cf := make(chan interface{})

channel uses the operator <- to send or receive data.

  1. ch <- v // send v to channel ch.
  2. v := <-ch // receive data from ch, and assign to v

Let’s see more examples.

  1. package main
  2. import "fmt"
  3. func sum(a []int, c chan int) {
  4. total := 0
  5. for _, v := range a {
  6. total += v
  7. }
  8. c <- total // send total to c
  9. }
  10. func main() {
  11. a := []int{7, 2, 8, -9, 4, 0}
  12. c := make(chan int)
  13. go sum(a[:len(a)/2], c)
  14. go sum(a[len(a)/2:], c)
  15. x, y := <-c, <-c // receive from c
  16. fmt.Println(x, y, x+y)
  17. }

Sending and receiving data in channels blocks by default, so it’s much easier to use synchronous goroutines. What I mean by block is that a goroutine will not continue when receiving data from an empty channel, i.e (value := <-ch), until other goroutines send data to this channel. On the other hand, the goroutine will not continue until the data it sends to a channel, i.e (ch<-5), is received.

Buffered channels

I introduced non-buffered channels above. Go also has buffered channels that can store more than a single element. For example, ch := make(chan bool, 4), here we create a channel that can store 4 boolean elements. So in this channel, we are able to send 4 elements into it without blocking, but the goroutine will be blocked when you try to send a fifth element and no goroutine receives it.

  1. ch := make(chan type, n)
  2. n == 0 ! non-bufferblock
  3. n > 0 ! buffernon-block until n elements in the channel

You can try the following code on your computer and change some values.

  1. package main
  2. import "fmt"
  3. func main() {
  4. c := make(chan int, 2) // change 2 to 1 will have runtime error, but 3 is fine
  5. c <- 1
  6. c <- 2
  7. fmt.Println(<-c)
  8. fmt.Println(<-c)
  9. }

Range and Close

We can use range to operate on buffer channels as in slice and map.

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func fibonacci(n int, c chan int) {
  6. x, y := 1, 1
  7. for i := 0; i < n; i++ {
  8. c <- x
  9. x, y = y, x+y
  10. }
  11. close(c)
  12. }
  13. func main() {
  14. c := make(chan int, 10)
  15. go fibonacci(cap(c), c)
  16. for i := range c {
  17. fmt.Println(i)
  18. }
  19. }

for i := range c will not stop reading data from channel until the channel is closed. We use the keyword close to close the channel in above example. It’s impossible to send or receive data on a closed channel; you can use v, ok := <-ch to test if a channel is closed. If ok returns false, it means the there is no data in that channel and it was closed.

Remember to always close channels in producers and not in consumers, or it’s very easy to get into panic status.

Another thing you need to remember is that channels are not like files. You don’t have to close them frequently unless you are sure the channel is completely useless, or you want to exit range loops.

Select

In the above examples, we only use one channel, but how can we deal with more than one channel? Go has a keyword called select to listen to many channels.

select is blocking by default and it continues to execute only when one of channels has data to send or receive. If several channels are ready to use at the same time, select chooses which to execute randomly.

  1. package main
  2. import "fmt"
  3. func fibonacci(c, quit chan int) {
  4. x, y := 1, 1
  5. for {
  6. select {
  7. case c <- x:
  8. x, y = y, x+y
  9. case <-quit:
  10. fmt.Println("quit")
  11. return
  12. }
  13. }
  14. }
  15. func main() {
  16. c := make(chan int)
  17. quit := make(chan int)
  18. go func() {
  19. for i := 0; i < 10; i++ {
  20. fmt.Println(<-c)
  21. }
  22. quit <- 0
  23. }()
  24. fibonacci(c, quit)
  25. }

select has a default case as well, just like switch. When all the channels are not ready for use, it executes the default case (it doesn’t wait for the channel anymore).

  1. select {
  2. case i := <-c:
  3. // use i
  4. default:
  5. // executes here when c is blocked
  6. }

Timeout

Sometimes a goroutine becomes blocked. How can we avoid this to prevent the whole program from blocking? It’s simple, we can set a timeout in the select.

  1. func main() {
  2. c := make(chan int)
  3. o := make(chan bool)
  4. go func() {
  5. for {
  6. select {
  7. case v := <-c:
  8. println(v)
  9. case <-time.After(5 * time.Second):
  10. println("timeout")
  11. o <- true
  12. break
  13. }
  14. }
  15. }()
  16. <-o
  17. }

Runtime goroutine

The package runtime has some functions for dealing with goroutines.

  • runtime.Goexit()

    Exits the current goroutine, but defered functions will be executed as usual.

  • runtime.Gosched()

    Lets the scheduler execute other goroutines and comes back at some point.

  • runtime.NumCPU() int

    Returns the number of CPU cores

  • runtime.NumGoroutine() int

    Returns the number of goroutines

  • runtime.GOMAXPROCS(n int) int

    Sets how many CPU cores you want to use