Historic archive

Concurrency in Go!

What is concurrency? Execution of multiple processes independantly and they may or may not be getting executed at the same time. Imagine a webserver gets multiple smaller requests and the…

VirtEngine by DET.io 2 minutes

A technician working on hardware in a server room.

What is concurrency?

Execution of multiple processes independantly and they may or may not be getting executed at the same time. Imagine a webserver gets multiple smaller requests and they all concurrently gets executed. It is basically multitasking on a single-core processor.

Oh, wait! then Concurrency is parallelism?

Parallelism is about running multiple processes at the same time, but concurrency is about dealing with a lot of processes may or may not be getting executed at the same time. Confusing?

Parallelism

  • Simultaneous Execution
  • Running multiple threads on different processors at the same time against each other.
  • Requires multiple cores.

Concurrency

  • Independent Execution
  • Running multiple light-weight concurrent processes(goroutines!!, hold on! we’ll see what goroutines are)
  • Single core

Note: Using GOMAXPROCS, it is possible to run goroutines on different logical processors by configuring the runtime.

So, ‘Concurrency is not parallelism’ but,

‘concurreny can achieve parallelism’.

why Go?

Go’s concurrency primitives are way too good and it is easy to write concurrent programs. Go uses goroutines to achieve concurreny and importantly, it makes communication between goroutines a lot easier.

what are goroutines ?

Fundamentally, goroutine is a function which runs concurrently with other functions.

So, here is an example

package main
import (
    "fmt"
       )

func print(s string){
        for i := 0; i < 5; i++ {
             fmt.Println(s)
     }
 }


 func main() {
     print("Current thread")

     go print("bye")  

     var input string
     fmt.Scanln(&input)
 }

Compile the above code to understand how the goroutines work. We’ll look into more details in a bit.

what are channels?

Channels are basically pipelines that are used to communicate between two goroutines. Channels can be used for synchronization of goroutines, etc.

package main

import (
    "fmt"
    "time"
    )

    func put(c chan string){
     for {
        c <- "Tadaa!"
       }
      }

    func print(c chan string){
      for {
        message := <- c
        fmt.Println(message)
        time.Sleep(time.second * 2)
        }
       }
     func main() {
       var c chan string = make(chan string)

       go put(c)
       go print(c)

       var input string
       fmt.Scanln(&input)
      }

How awesome right? Thats how cool goroutines and channels are. It makes it a lot easier and more interesting to program.

why are goroutines super cool?

Concurreny was possible in other languages too, but what made concurreny a cherrypie in go?

goroutines are light-weight, other languages like java, uses threads.

Creating a goroutine hardly takes 2kb of stackspace(it was 8kb till go 1.3) where as a thread consumes 1MB per stack space.

I recommend all of you reading this to watch Go Concurrency Patterns by Rob Pike

Few Links:

Fundamentals of concurrent programming

Concurreny goroutines and GOMAXPROCS

Continue reading

More from the archive

A padlock and key — bonded stake under slash conditions.
Historic archive

Ceph in a single node cluster

Ceph is one of the most interesting distributed storage systems available, with a very active development and a complete set of features that make it a valuable candidate for cloud storag…

Server racks in a provider data centre.
Historic archive

Hadoop/Spark multi-node setup

This is an installation guide for Apache Hadoop and Apache Spark on a multi node setup. We will use Cloudera v5 as it has the packages for debian. You are free to use Hortonworks ,MapR (o…