[Journey with golang] 5. Concurrent

Concurrency and parallelism are two different concepts.

  • Parallel means that programs are run simultaneously at any time
  • Concurrent means that the program in a unit time is running simultaneously

Parallel is a particle size within any time the ability to simultaneously execute, in parallel is more simple, multi-parallel processing machines. SMP surface is parallel, it is a shared memory, and synchronization between threads, etc., can not be completely done in parallel.

Concurrency is specified multiple requests have been executed within the time and treatment, and that is the feeling to the outside world, in fact, may be sharing internal operations. Concurrent focuses avoid blocking, the program will not be suspended from their duties because of a blockage. Concurrent typical application scenarios: time-sharing operating system is a kind of concurrent design (ignore the multi-core CPU).

Parallel is a problem of hardware and operating system developers an important consideration, as the programmer of the application layer, the only choice is to make full use of API and programming language features of the operating system provides, combined with the actual needs of concurrent design a program with good structure, improve program of concurrent processing capability. The most basic concurrency model modern operating system is able to provide multi-threaded and multi-process; programming language at this level can further enhance the package to concurrent processing program.

In current computer systems, parallel with transient, having a complicated procedural; complicated in the structure, wherein the parallel execution. Concurrent application with good structure, the operating system to make better use of parallel execution hardware while avoiding blocking wait, reasonable scheduling, improve CPU utilization. An important means to enhance the application layer programmer program concurrent processing capability is for the good of concurrent programming structure.

Operating systems can schedule threads and processes, itself has concurrent processing capability, but the process of switching cost is still too high, the process of switching the need to preserve the scene, the more time-consuming. If the application can then schedule the user to build a layer, the particle size is further reduced concurrent, perhaps to a greater extent enhance the process efficiency. golang concurrent implementation is based on this idea. golang in concurrent mode supports this language level.

Concurrent execution golang is called goroutine, routine translation is routine, so go goroutine called routine will be more reasonable. golang initiated by keyword go a goroutine, go back keywords must be followed by a function, not a statement or something else, the return value of the function is ignored.

  • + Goroutine promoter functional form by anonymous go, the following code:
     1 package main
     2 
     3 import (
     4     "fmt"
     5     "runtime"
     6     "time"
     7 )
     8 
     9 func main() {
    10     go func() {
    11         sum := 0
    12         for i := 0; i < 1000; i++ {
    13             sum += i
    14         }
    15         fmt.Println(sum)
    16         time.Sleep(1 * time.Second)
    . 17      } ()
     18 is      // NumGoroutine can return to the current program number goroutines 
    . 19      fmt.Println ( " NumGoroutine = " , runtime.NumGoroutine ())
     20 is      // main goroutines deliberately prevented seconds sleep early exit 
    21 is      time.sleep ( . 3 * time.Second)
     22 is }
  • + Goroutine promoter functional form by known go, the following code:
     1 package main
     2 
     3 import (
     4     "fmt"
     5     "runtime"
     6     "time"
     7 )
     8 
     9 func sum() {
    10     sum := 0
    11     for i := 0; i < 1000; i++ {
    12         sum += i
    13     }
    14     fmt.Println(sum)
    15     time.Sleep(1 * time.Second)
    16 }
    17 
    18 func main() {
    19     go sum()
    20     fmt.Println("NumGoroutine =", runtime.NumGoroutine())
    21     time.Sleep(3 * time.Second)
    22 }

goroutine has the following features:

  • Execution go is non-blocking, not wait
  • The return value of the function go back will be ignored
  • The scheduler can not guarantee the execution order of the plurality goroutine
  • Goroutine no concept of father and son, all goroutine is equally scheduled and executed
  • Go to create additional goroutine go when the program will create a separate function for the main goroutine, go meet other keywords perform
  • go goroutine not exposed to the user ID can not be displayed in a manner which goroutine goroutine another, but the runtime package provides some information access functions and settings of goroutine
  1. depending GOMAXPROCS
    1.  GOMAXPROCS FUNC (n- int ) int  is used to set or query the number goroutine can execute concurrently, n is greater than 1 indicates GOMAXPROCS set value, or the current query indicates GOMAXPROCS value.
      . 1  Package main
       2  
      . 3  Import (
       . 4      " FMT " 
      . 5      " Runtime " 
      . 6  )
       . 7  
      . 8  FUNC main () {
       . 9      // Get the current value of GOMAXPROCS 
      10      fmt.Println ( " GOMAXPROCS = " , runtime.GOMAXPROCS ( 0 ))
       . 11      // set GOMAXPROCS value of 2 
      12 is      runtime.GOMAXPROCS ( 2 )
       13 is      // Get the current value of GOMAXPROCS 
      14      fmt.Println ( " GOMAXPROCS = " , runtime.GOMAXPROCS (0))
      15 }
  2. depending Goexit
    1.  func Goexit ()  is to end the current run of goroutine, Goexit before the end of the current goroutine calls defer current goroutine already registered, Goexit does not create panic, so that goroutine defer inside recover call returns nil.
  3. func Gosched
    1.  func Gosched ()  is currently executing abandon opportunistic scheduling, the current goroutine into the queue waiting for the next scheduled.

Goroutine only is not enough, the communication between a plurality of goroutine needed, synchronization, collaboration functions.

Guess you like

Origin www.cnblogs.com/JHSeng/p/12214964.html