zoukankan      html  css  js  c++  java
  • https://github.com/golang/go/wiki/CommonMistakes

    CommonMistakes

    https://golang.org/doc/faq#closures_and_goroutines

    Why is there no goroutine ID? 

    Goroutines do not have names; they are just anonymous workers. They expose no unique identifier, name, or data structure to the programmer. Some people are surprised by this, expecting the go statement to return some item that can be used to access and control the goroutine later.

    The fundamental reason goroutines are anonymous is so that the full Go language is available when programming concurrent code. By contrast, the usage patterns that develop when threads and goroutines are named can restrict what a library using them can do.

    Here is an illustration of the difficulties. Once one names a goroutine and constructs a model around it, it becomes special, and one is tempted to associate all computation with that goroutine, ignoring the possibility of using multiple, possibly shared goroutines for the processing. If the net/http package associated per-request state with a goroutine, clients would be unable to use more goroutines when serving a request.

    Moreover, experience with libraries such as those for graphics systems that require all processing to occur on the "main thread" has shown how awkward and limiting the approach can be when deployed in a concurrent language. The very existence of a special thread or goroutine forces the programmer to distort the program to avoid crashes and other problems caused by inadvertently operating on the wrong thread.

    For those cases where a particular goroutine is truly special, the language provides features such as channels that can be used in flexible ways to interact with it.

  • 相关阅读:
    BZOJ1930 [Shoi2003]pacman 吃豆豆
    hdu5322 Hope
    hdu5390 tree
    hdu4609 3-idiots
    hdu5354 Bipartite Graph
    hdu4918 Query on the subtree
    hdu5314 Happy King
    [题解] PowerOJ 1741 最长递增子序列问题 (最大流)
    [题解] PowerOJ 1740 圆桌问题 (最大流)
    [题解] PowerOJ 1739 魔术球问题 (最大流)
  • 原文地址:https://www.cnblogs.com/rsapaper/p/12168666.html
Copyright © 2011-2022 走看看