How do I get the ID of goroutine?

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

When using Java it is easy to get the name of the thread, such as "Thread.CurrentThread (). GetName", so that you can do some monitoring operations or set some thread-related data. When it turned to Golang development, it found that the go language did not provide an operation to get the current Goroutine ID. This is Golang developers deliberately, to avoid developers abusing Goroutine ID implementation Goroutine Local storage (Java-like "thread-local" storage), because Goroutine Local Storage It is difficult to do garbage collection. So despite the previous exposure to the corresponding method, it has now been hidden.

Please don ' t use goroutine local storage. It ' s highly discouraged. In fact, IIRC, we used-expose Goid, but it's hidden since we don ' t want people to does this.

Potential problems include:

  1. when Goroutine goes away, its goroutine local storage won ' t is GCed. (You can get goid for the current goroutine, but can ' t get a list of all running Goroutines)
  2. What if handler spawns Goroutine itself? The new goroutine suddenly loses access to your goroutine local storage. You can guarantee this your own code won ' t spawn other goroutines,
    If you can do sure the standard LIBR ary or any 3rd party code won ' t does that.

Thread Local storage is invented to help reuse bad/legacy code that assumes global state, Go doesn ' t has legacy Code like this, and you really should design your code so the state was passed explicitly and not as global (e.g. resort To Goroutine Local storage)

Of course, this hidden practice of go is still controversial, a bit unworthy. The Goroutine ID is a good monitoring information when debugging log. This article describes two ways to get Goroutine IDs.

The current Goroutine ID can be obtained in the pure Go language. The code looks like this:

12345678910111213141516171819202122232425262728293031323334
 PackageMainImport("FMT""Runtime""StrConv""Strings""Sync")funcGoID ()int{varBuf[ +]byteN: = runtime. Stack (buf[:],false) IDfield: = Strings. Fields (strings. Trimprefix (string(Buf[:n]),"Goroutine"))[0]id, err: = StrConv. Atoi (IDfield)ifErr! =Nil{Panic(FMT. Sprintf ("Cannot get Goroutine ID:%v", err))}returnIdfuncMain () {FMT. Println ("Main", GoID ())varWG Sync. Waitgroup forI: =0; I <Ten; i++ {i: = IWG. Add(1)Go func() {deferWg. Done () fmt. Println (i, GoID ())} ()}wg. Wait ()}

go run main.goOutput:

 1234567891011 
 main  1  9  14  0  5  1  Span class= "number" >6  2  7  5  10  6  11  3  8  7  12  4  9  8  13  

It leverages runtime.Stack the stack information. runtime.Stack(buf []byte, all bool) intwrites the current stack information to a slice, and the first behavior of the stack goroutine #### [… , which #### is the current Gororutine ID, is implemented by this gimmick GoID .

However, it is important to note that getting stack information can affect performance, so it is recommended that you use it when you debug.

Resources

    1. Https://groups.google.com/forum/#!topic/golang-nuts/Nt0hVV_nqHE
    2. Http://wendal.net/2013/0205.html
    3. http://blog.sgmansfield.com/2015/12/goroutine-ids/
    4. Http://dave.cheney.net/2013/09/07/how-to-include-c-code-in-your-go-package
    5. http://golanghome.com/post/566
    6. Https://github.com/t-yuki/goid
    7. Https://github.com/petermattis/goid.git
    8. Https://github.com/huandu/goroutine
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.