As far as I know, Go runtime scheduler manages some number of OS Threads(probably more than GOMAXPROCS?) and Go routines by assigning Go routines to OS Threads continuously.
So this basically means that the execution of Go routines, including main goroutine , are managed by both of go scheduler and OS' thread scheudling.
Now here's my questions..
Does the execution of goroutine fully managed by OS' thread scheduling if I call
runtime.LockOSThread()
at the start of that goroutine?Does the execution of non-Go thread also fully managed by OS' thread scheduling? In other words, if I create a non-Go Thread by
CreateThread
function (Windows), then the management non-Go Thread's execution is out of scope of Go's runtime scheduler?What if I launch another goroutine with
go func()
in that non-Go Thread? How that non-Go Thread and goroutine's execution is managed?-
Currently, I'm writing a program in Golang which runs a windows message loop in
main()
function of go program. Most of the time it worked well, but sometimes the message loop get blocked and resumed after few seconds and then large amount of old messages get pumped. (My another question: Windows Message Loop is getting blocked and resumed intermittently (golang))I had no idea why it occurs, so I suspected main goroutine's OS Thread switch by go scheduler. So I added
runtime.LockOSThread()
at the start ofmain()
function to ensure windows message loop always run in the same thread. However, the problem still occured!I still have no idea why it occurs, but I'm suspecting this is because of Go scheduler because the same logic written in Python 3.4 didn't make any problems like this.
So what I'm trying now is creating a new Windows Thread (non-Go Thread) by calling
CreateThread(...)
function, and running windows message loop in that thread.But I'm curious that whether this approach is different with calling
runtime.LockOSThread()
in main goroutine running windows message loop from Go runtime scheduler's perspective.So my question is, 'If I create a new non-Go Thread with
CreateThread(...)
function and run windows message loop in that thread, then does execution of that thread not affected by Go's runtime scheduler?'
Any helps or ideas will be greatly appreciated. Thanks.