jneo8.com

Search

Search IconIcon to open search

Golang goroutines-202301071522

Last updated Jan 7, 2023 Edit Source

# Golang goroutines-202301071522

A note describe how goroutine works

# Notes

# Concurrency is not Parallelism

# What are threads?

# Linux, task, process, thread

Linux task, process, thread-202301071513

# What make threads slow?

# What about Goroutines?

Goroutines exist only in the virtual space of the Go runtime and not the OS, therefore the Go Runtime scheduler is needed to manage their life-cycles.

flowchart TD subgraph go-binary go-program go-runtime go-program <--Memory Allocation--> go-runtime go-program <--Channel communication--> go-runtime go-program <--Creation of goroutines--> go-runtime end OS-Kernel go-runtime --syscalls--> OS-Kernel go-runtime --Thread creation--> OS-Kernel

Go Runtime maintains four C structs for this purpose:

There are two queues contain G Struct:

There is only one queue pertaining to M’s (threads) that the scheduler maintains. And in order to modify these queues, the Global Sched Lock must be held.

flowchart TD %% class classDef os-scheduler fill:#808000,color:#000; classDef cpu fill:#98AFC7,color:#000; classDef thread fill:#667C26,color:#000; classDef scheduler fill:#488AC7,color:#000; classDef global-run-queue fill:#bbf,stroke:#f66,stroke-width:2px,color:#000,stroke-dasharray: 5 5; classDef local-run-queue fill:#f9f,stroke:#333,stroke-width:4px,color:#000; OS-Scheduler:::os-scheduler Logical-CPU-1:::cpu Logical-CPU-2:::cpu OS-Thread-1:::thread OS-Thread-2:::thread OS-Thread-3:::thread Go-Scheduler:::scheduler Global-Run-Queue:::global-run-queue Local-Run-Queue-1:::local-run-queue Local-Run-Queue-2:::local-run-queue OS-Scheduler{OS-Scheduler} Logical-CPU-1(Logical-CPU-1) Logical-CPU-2(Logical-CPU-2) OS-Thread-1([OS-Thread-1: M]) OS-Thread-2([OS-Thread-2: M]) OS-Thread-3([OS-Thread-3: M]) Processer-1(Processer-1: P) Processer-2(Processer-2: P) Go-Scheduler{Go-Scheduler: Sched} Global-Run-Queue(Global-Run-Queue) Local-Run-Queue-1(Local-Run-Queue-1) Local-Run-Queue-2(Local-Run-Queue-2) G1((G1: G)) G2((G2: G)) G3((G3: G)) G4((G4: G)) G5((G5: G)) G6((G6: G)) G7((G7: G)) G8((G8: G)) G9((G9: G)) G10((G10: G)) GX1((...)) GX2((...)) GX3((...)) subgraph kernel-space OS-Threads OS-Scheduler Logical-CPU-1 Logical-CPU-2 end subgraph user-space GOMAXPROCS Local-Run-Queue-1 Local-Run-Queue-2 Go-Scheduler Global-Run-Queue end subgraph Global-Run-Queue G7 --> G8 --> G9 --> G10 --> GX3 end subgraph Local-Run-Queue-1 G1 --> G2 --> G3 --> GX1 end subgraph Local-Run-Queue-2 G4 --> G5 --> G6 --> GX2 end subgraph GOMAXPROCS Processer-1 Processer-2 end subgraph OS-Threads OS-Thread-1 OS-Thread-2 OS-Thread-3 end OS-Scheduler ----> Logical-CPU-1 OS-Scheduler ----> Logical-CPU-2 Logical-CPU-1 --> OS-Thread-1 Logical-CPU-2 --> OS-Thread-2 Logical-CPU-2 --> OS-Thread-3 Go-Scheduler ==> Global-Run-Queue OS-Thread-1 -.-> Processer-1 -.-> Local-Run-Queue-1 OS-Thread-2 -.-> Processer-2 -.-> Local-Run-Queue-2 OS-Thread-3 -.-> Processer-2

# So here is the catch of Goroutines


# References