Home  >  Article  >  Backend Development  >  Golang context switching

Golang context switching

WBOY
WBOYforward
2024-02-06 09:10:08819browse

Golang 上下文切换

Question content

I have a very basic question: Can the operating system scheduler context switch between user-level threads mapped to kernel level?

Assume that goscheduler schedules a goroutine A on the kernel thread. Now goroutine A makes a sysblock call, who does the context switch first: the OS or the go scheduler?


Correct answer


The Go scheduler manages goroutines, which are reused on operating system-level threads. The Go scheduler can efficiently perform context switches between Goroutines, such as when a Goroutine makes a blocking system call, without involving the OS scheduler. However, it is important to note that due to factors such as time slicing, the operating system scheduler may independently interrupt and perform context switches. Therefore, the exact timing of a context switch and the involvement of the operating system scheduler may not be accurately determined from the Go program's perspective.

Further reading:

proc.go in golang source code

Go Scheduler: Implementing a lightweight concurrency language

The above is the detailed content of Golang context switching. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete