Goroutines

Communicating Sequential Processess (CSP)

  • Each process a built for sequential execution.
  • Data is communicated betewwn processes. No shared memory
  • Scale by adding more of the same
  • 在 n 個操作系統線程上多工調度 m 個 Goroutines

初始化順序

同一個文件可以有多個 init() function 依出現順序執行, 但是不能被其他函數調用. 初始化順序

Go's Concurrency Tool Set

  • goroutines
  • channels
  • select
  • sync package

Goroutines

  • Goroutines like user space threads managed by go runtime
  • Goroutines extremely lightweight. Goroutines start with 2KB of stack, which grows and shrinks as required. (thread- 8MB of stack)
    • 2kb ~ 1GB 的 stack 大小(動態的)
  • Low CPU overhead - three instructions per function call.
  • Can create hundreds of thounsands of goroutines in the same address space.
  • Channels are used for communication of data between goroutines. Sharing of memory can be avoided.
  • Context switching between goroutines is much cheaper than thread context switching.
  • Go runtime can be more selective in what is persisted for retrievl, how it is persisted, and when the persisting needs to occur.
  • Go runtime creates worker OS threads.
  • Goroutines runs in the context of OS thread.
  • Goroutines & Closures can directly modify variables in the enclosing lexical block. Image Image

https://www.udemy.com/course/concurrency-in-go-golang/

© Kimi Tsai all right reserved.            Updated : 2023-07-12 09:04:54

results matching ""

    No results matching ""

    results matching ""

      No results matching ""