This is a creation in Article, where the information may have evolved or changed.
Go routine indeed
This short conclusion is quoted from: Goroutine behind the system knowledge, let me understand why goroutine so lightweight, and its advantages and disadvantages.
The go language provides the clearest and most straightforward support for concurrent programming in all (I know) languages so far through Goroutine, and the Go language document describes its features in a very comprehensive or even more detailed way, based on our system knowledge above, To enumerate the characteristics of Goroutine is a summary:
(1) Goroutine is the function of the go language runtime, not the functionality provided by the operating system, Goroutine is not implemented with threads. Refer to the PKG/RUNTIME/PROC.C in the Go language source
(2) Goroutine is a piece of code, a function entry, and a stack allocated to it on the heap. So it's very cheap and we can easily create tens of thousands of goroutine, but they're not executed by the operating system.
(3) In addition to a thread that is blocked by the system call, the Go runtime will start up to $gomaxprocs threads to run Goroutine
(4) Goroutine is a cooperative scheduling, if the goroutine will take a long time, and not by waiting to read or write to the channel data to synchronize, you need to actively call gosched () to let the CPU
(5) As in all other concurrent frames, the goroutine advantage of the so-called "no lock" is only valid on a single thread, and if $gomaxprocs > 1 and the co-process need to communicate, the Go runtime is responsible for lock-protected data. This is why sieve.go such an example is slower in multi-CPU multi-Threading
(6) The Web and other service-side programs to deal with the request is essentially a parallel processing problem, each request is basically independent, non-dependent, almost no data interaction, this is not a model of concurrent programming, and the concurrent programming framework only solves the complexity of its semantic expression, not fundamentally improve the efficiency of processing, Perhaps the English of concurrent connections and concurrent programming is concurrent, and it is easy to create a misunderstanding that the concurrent programming framework and coroutine can handle a large number of concurrent connections efficiently.
(7) The Go Language runtime encapsulates asynchronous IO, so it is possible to write a server that looks like a lot of concurrency, but even if we tune $gomaxprocs to take advantage of multi-core CPU parallelism, it is not as efficient as we use IO event-driven design to divide the appropriate proportion of thread pools by transaction type. In response time, collaborative scheduling is flawed.
(8) Goroutine The biggest value is that it realizes the concurrent and actual parallel execution of the thread mapping and dynamic expansion, along with the development and improvement of its running library, its performance will be better, especially in the CPU more and more of the future of the number of cores, One day we will give up the difference in performance for the simplicity and maintainability of the code.
Start a Go routine
Go keyword + function name to start a go routine:
Package Mainimport ("FMT", "Time") Func p () {for I: = 0; i <; i++ { fmt. Println (i) Time . Sleep (time. Second * 1) }}func Main () { go P () var input string fmt. Scanln (&input) fmt. Println ("End")}
Goroutine Communication: Channel
Go routine uses the channel to communicate between routine:
Package Mainimport ("FMT", "Time" "Math/rand") Func sell (c Chan int) {for {num: = <-cfmt. Println ("Sell", num, "bread")}}func produce (c Chan int) {for {num: = rand. INTN (Ten) T: = time. Duration (num) fmt. Println ("Product", num, "bread") c <-numtime. Sleep (time. second* t)}}func Main () {var c chan int = make (chan int) go sell (c) go Produce (c) var input string fmt. Scanln (&input) fmt. Println ("End")}//output: Product 1 breadsell 1 breadproduct 7 Breadsell 7 Breadproduct 7 Breadsell 7 breadproduct 9 breadsell 9 Bread
The default channel is bidirectional and can also be defined as one-way at the function entry:
Func sell (c <-chan int) //can only be removed from the channel func produce (c chan<-int)//channel can only be placed in
A SELECT statement is used to select a ready channel in multiple channels, such as:
Select {Case MSG1: = <-C1: FMT. Println ("Message 1", MSG1) case MSG2: = <-C2: fmt. Println ("Message 2", MSG2) case <-time. After (time. Second): FMT. PRINTLN ("timeout") Default: FMT. Println ("Nothing Ready")}
Time. After will create an anonymous channel after the specified time to wait for a timeout. If all channel is not ready, execute the default block immediately. Specifying the second parameter on make channel creates a buffer channel similar to the fixed-length queue in other high-level languages:
c: = make (chan int, 1)