Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/sys: windows.GetCurrentThreadId() returns same thread id. #40386

Closed
burakkoken opened this issue Jul 24, 2020 · 2 comments
Closed

x/sys: windows.GetCurrentThreadId() returns same thread id. #40386

burakkoken opened this issue Jul 24, 2020 · 2 comments

Comments

@burakkoken
Copy link

burakkoken commented Jul 24, 2020

go 1.13

When I create more than one go routine and use the calling of windows.GetCurrentThreadId function in their function body, it returns same thread id. Note: go routine body contains time.Sleep() not to be stop its execution immeditately. I mean all go routines are still executed.

go func() {
   threadId := windows.GetCurrentThreadId()
   log.Print(fmt.Sprintf("Go Routine 1 : %d", threadId ))
   time.Sleep(10E9)
}()
...
go func() {
   threadId := windows.GetCurrentThreadId()
   log.Print(fmt.Sprintf("Go Routine 10 : %d", threadId ))
   time.Sleep(10E9)
}()
@gopherbot gopherbot added this to the Unreleased milestone Jul 24, 2020
@davecheney
Copy link
Contributor

Go uses a user space scheduling model whereby a large number of goroutines are mapped onto a small (usually on the order of the number of cores your operating system exposes) number of operating system threads. When a goroutine executes time.Sleep, it is no longer runnable (obviously) and as part of going to sleep to yields back to the scheduler so that the thread can service other runnable goroutines.

It is possible (although I can't test it as I don't have access to a windows system) that over time calling windows.GetCurrentThreadId() will return essentially random values. To avoid this some users call runtime.LockOSThread to reserve the current thread for only the runnable goroutine.

Thank you for raising this issue. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@davecheney davecheney removed this from the Unreleased milestone Jul 24, 2020
@burakkoken
Copy link
Author

burakkoken commented Jul 24, 2020

Thanks for your answer, It is totally obvious for me. But I would like to ask a question another. I know here is not place for discussion. Let's say we have a running go routine in a thread. Does Go allow to be executed another go routine in the same thread before running thread does not end? Do I have to use runtime.LockOSThread function to reserve the thread?

@golang golang locked and limited conversation to collaborators Jul 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants