Go learn it from scratch concurrency (a): Go away

Go Cheng (goroutine) is managed by the Go run when lightweight threads.

When a goroutine creation, and will run concurrently with other goroutine, scheduling and management goroutine run by the Go program when.

Go program () function starts from main main pack, when the program starts, the program will Go () function creates a default for the main goroutine

All goroutine in main () will end with the end of the function.

Go away run in the same address space, it must be synchronized when accessing shared memory.

 

create:

go function name (parameter list)

go say("world")

When you create a goroutine use go keyword, the called function's return value is ignored.

To return the goroutine data, use channel described later (channel) characteristic, by the data channel as the return value from the outgoing goroutine.

 

Creating an anonymous function:

go func (parameter list) {

Function body

} (Call parameter list)

go func(s string) {
 for i := 0; i < 5; i++ {
  time.Sleep(100 * time.Millisecond)
  fmt.Println(s)
 }
}("hello")

 

Concurrent adjustment of operating performance:

Go may be ground () function is done by runtime.GOMAXPROCS

runtime.GOMAXPROCS (number of logical CPU)

Here the number of logical CPU can have several values ​​are as follows:

<1: does not modify any value

= 1: single-core execution

> 1: Multicore concurrently

Under normal circumstances, it can be used runtime.NumCPU () number of queries the CPU, and runtime.GOMAXPROCS () function is provided, for example:

runtime.GOMAXPROCS(runtime.NumCPU())

Starting Go 1.5 version, the default implementation of the above statements in order to make the code execute concurrently, the maximum efficient use of CPU.

Guess you like

Origin www.cnblogs.com/VingB2by/p/11119866.html