Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Golang use goroutine which is similar with thread, but little bit different. Goroutine can make you easy to use thread with concurrency. Also goroutine makes your program faster and lighter than when using original OS thread.

Basically, goroutine and OS thread are same in how they share their resoruces(Heap, Data, Code section of memory) in process

  • Code section
    • Store whole binary code
  • Data section
    • Store Global variable, static variable, array, structure
  • Heap section
    • With malloc & free or new & delete, structure or variable are allocated & return
  • Stack section
    • Store local variable, argument variable when function called

Now, here is a difference of OS thread and goroutine.

Goroutine

Goroutine is M:N thread

goroutine

  • G (Goroutine) : Goroutine
    • has Stack Pointer, Program Counter, DX
  • M(Machine) : OS thread
    • has pointer of P
    • run G by P’s LRQ
  • P(Processor) : logical processor
    • has one LRQ
    • allocate G in M
    • in case that if G access to locked resources, G should wait until that resource is unlocked. P re-allocate G into left M, so that wait in other thread

      to show block is never happened

  • LRQ(Local run queue) : Run Queue
    • P pop G from its LRQ and allocate to M
    • every P has its own LRQ
  • GRQ(Global run queue) : Run Queue
    • if every LRQ full, G is stored in GRQ

Memory usage

  • goroutine
    • need 2KB stack

      as G only need SP, PC, DX

  • thread(Java)
    • need more than 1MB stack

      (i.e. 16 general purpose registers, PC, SP, segment registers etc.)

Context Switch

  • goroutine
    • as goroutine is 2KB, context switch cost is very cheap
    • Also as context switch is very easy, concurrency performance is higher than original OS thread
  • thread(JAVA)
    • context switch cost is expensive
    • Java use OS thread as their abstract thread, concurrency performance is lower than

References