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

faq: document why there is no way to get a goroutine ID #22770

Closed
ianlancetaylor opened this issue Nov 16, 2017 · 5 comments
Closed

faq: document why there is no way to get a goroutine ID #22770

ianlancetaylor opened this issue Nov 16, 2017 · 5 comments

Comments

@ianlancetaylor
Copy link
Contributor

We've deliberately decided that there should be no way to get a goroutine ID, and there is no goroutine local storage. Since this comes up from time to time, we should add the explanation to the FAQ.

Here is an explanation I just wrote which may serve as a starting point:

It's because Go makes it cheap to start new goroutines, and it is normal in Go for a single task to be implemented by a collection of goroutines. Some of those goroutines may be newly started, some may be long running goroutines that provide some service. Of course one must aggregate log entries and other information for a single task, but because multiple goroutines are routinely involved in performing a single task, using a goroutine ID is the wrong approach. It is a temptation that one may naturally reach for when starting to write a program that uses a single goroutine, but that then breaks as the program evolves to use multiple goroutines. Just as Go prohibits things like unused imports, it prohibits this temptation. The intent is to force you to start writing your program the way it is going to wind up anyhow.

There are obviously many other approaches and the point is that they work with multiple goroutines and they are explicit in the program source rather than implicit in the program execution.

@ianlancetaylor ianlancetaylor added this to the Go1.10 milestone Nov 16, 2017
@robpike robpike self-assigned this Nov 16, 2017
@bradfitz
Copy link
Contributor

There are additional reasons. The C pain of thread local storage infecting users of libraries should be mentioned.

@robpike
Copy link
Contributor

robpike commented Nov 17, 2017

@bradfitz Please expand.

@bradfitz
Copy link
Contributor

A classic example is graphics libraries that require that all operations be performed on "the main thread", because that's where some library stashed away its state, in TLS, and if you call from any other thread, you crash. It's why we have gunk like https://golang.org/pkg/runtime/#LockOSThread. It's why tracing is both magic & prone to break mysteriously inside Google in Java and C++, because it's magic until it no longer works at all, if you do something on the wrong thread.

In Go, we want to make sure regular code can create & use & forget about goroutines casually, and be able to call any code from any goroutine, without treating any particular goroutines as special.

@bronze1man
Copy link
Contributor

bronze1man commented Nov 17, 2017

  • By the way you can always get a thread id from the operation system with syscall, and use "the thread id" to work with graphics libraries "the main thread" stuff. Let C stuff work with C stuff. I think this message should go to your faq document.

  • I used to come across a problem that I want to dispatch a callback into windows api main thread, and I want to know weather the caller is current thread. I tried to found the goroutine ID. Then I just found i need use the windows api to get thread id.

@gopherbot
Copy link

Change https://golang.org/cl/80195 mentions this issue: doc/faq: explain why goroutines are anonymous

@golang golang locked and limited conversation to collaborators Nov 29, 2018
@rsc rsc unassigned robpike Jun 23, 2022
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

5 participants