Goroutines: the dark side of the runtime

  • GoRountines are fast but nothing comes for free
  • Closures + GoRuntines = :(
  • Closures variables evaluated when GoRoutine is run
  • GoRoutines scheduler tries not to context swtich
  • Channels are a thread safe way to send messages but be careful when reading
  • Runtime schedules same number of GoRoutines as logical processors
  • Runtime isn't preemptive
  • Checkpoints are baked in at compile time
  • Garbage collector is mostly synchronous
  • If GC kicks in and you have a GoRoutine that wont reach a checkpoint everything else stops and waits for it forever
  • Only way to fix this is to force the compiler to emit checkpoints
it is not specified by the standard when checkpoints are emitted,but some checkpoints that will likely never go away are:
- channels operation and select statments
- go statements
- i/o and syscalls
- call runtime.Gosched()
- non-inlined function calls //go:noinline

[please do not rely on this list as it is incomplete and might change in future versions]

复制代码
  • No way to stop a GoRoutine when its running
  • Need to communicate with them with channels