golang的线程源代码在runtime2.go中的m结构体。注意此处的线程是golang中的线程,不是操作系统中的线程,操作系统的线程由操作系统来调度。
type m struct {
g0 *g // goroutine with scheduling stack
morebuf gobuf // gobuf arg to morestack
divmod uint32 // div/mod denominator for arm - known to liblink
_ uint32 // align next field to 8 bytes
// Fields not known to debuggers.
procid uint64 // for debuggers, but offset not hard-coded
gsignal *g // signal-handling g
goSigStack gsignalStack // Go-allocated signal handling stack
sigmask sigset // storage for saved signal mask
tls [tlsSlots]uintptr // thread-local storage (for x86 extern register)
mstartfn func()
curg *g // current running goroutine
caughtsig guintptr // goroutine running during fatal signal
p puintptr // attached p for executing go code (nil if not executing go code)
nextp
Golang中的线程机制解析

本文探讨了Golang中的线程实现,不同于操作系统层面的线程,Golang使用m结构体在`runtime2.go`中进行管理。重点关注g0、curg和mos,g0负责调度器操作,curg是当前运行的协程(g)的指针,mos则存储着与操作系统线程相关的信息。
最低0.47元/天 解锁文章
913

被折叠的 条评论
为什么被折叠?



