10.Go-goroutine, waitgroup, mutex, channel and select

10.1.goroutine

goroutine use

//Learn_Go/main.go 
Package main 

Import ( 
	"FMT" 
	"Time" 
) 

FUNC Demo (COUNT int) { 
	for I: =. 1; I <10; I ++ { 
		fmt.Println (COUNT, ":", I) 
	} 
} 

FUNC main () { 
	for I: =. 1; I <10; I ++ { 
		Go Demo (I) 
	} 
	// add sleep time to wait for the end of execution goroutine 
	time.sleep (3E9) 
}

10.2.waitgroup

WaitGroup literally waiting for the group, in fact, counter, as long as the counter has been blocked content

There are three ways WaitGroup

  • Add (delta int) indicates adding an increment (delta) to the internal counter, which can make the negative parameter delta
  • DONE () indicates a decrease waitgroup counter value, the program should be executed in the last, corresponding to Add (-1)
  • Wait () indicate obstruction know waitgroup counter 0
//Learn_Go/main.go
package main

import (
	"fmt"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	wg.Add(5)
	for i := 0; i < 5; i++{
		go func(j int) {
			fmt.Println("第",j,"次执行")
			wg.Done()
		}(i)
	}
	wg.Wait()
	fmt.Println("程序结束")
}

10.3. Mutex and read-write locks

(1) a mutex

You can use the contents of sync.Mutex lock, mutex usage scenarios

  • A plurality gouroutine access the same function code segment
  • Operating a global variable
  • In order to ensure the security of shared variables, the value of security

 (2) read-write lock

Go language map is not thread safe, simultaneous operation of multiple gouroutine error may occur

RWMutex plurality of read locks may be added or a write lock, write lock can not coexist

map requires concurrent read in conjunction with the read-write lock is completed

Mutex lock code indicates the same time only one goroutine operation, read and write operations indicates the write lock in the lock range of data

//Learn_Go/main.go
package main

import (
	"fmt"
	"sync"
)

func main() {
	var rwm sync.RWMutex
	var wg sync.WaitGroup
	wg.Add(10)
	m := make(map[int]int)
	for i := 0; i < 10; i++{
		go func(j int) {
			rwm.Lock()
			m[j] = j
			fmt.Println(m)
			rwm.Unlock()
			wg.Done()
		}(i)
	}
	wg.Wait()
	fmt.Println("程序结束")
}

10.4.channel

channel is in the process of communication, each channel can only pass a value type, you need to specify the type when declaring channel

The main role of the two channel in Go: synchronization and communication

(1) declare channel syntax

  • var name chan Type
  • var name chan <- Write-only type
  • var name <- chan type readonly
  • Name: = make (chan int) unbuffered chanel
  • Name: = make (chan int) unbuffered channel
  • Name: = make (chan int, 100) has a buffer channel

 (2) the operation of the channel grammar

  • ch <- add a value to the value of the ch
  • <- ch ch retrieves a value from the
  • a: = <-ch removed from a value assigned to a ch and
  • a, b: = <-ch ch removed from a value assigned to a, if ch ch closed or no value, b is false,

 (3) whether the data stored in the channel will block or fetch data

//Learn_Go/main.go
package main

import "fmt"

func main() {
	ch := make(chan int)
	go func() {
		fmt.Println("执行")
		ch <- 111     
	}()
	a := <- ch
	fmt.Println(a)
	fmt.Println("程序结束")
}

 (4) using the channel for communication between gouroutine

//Learn_Go/main.go 
Package main 

Import "FMT" 

FUNC main () { 
	CHl: = the make (Chan String) 
	CH2: = the make (Chan int) 

	Go FUNC () { 
		CHl <- "Derek" 
		CH2 <- 111 
	} () 

	Go FUNC () { 
		Content: = <- CHl 
		fmt.Println ( "fetch data:", content) // fetch data: Derek 
		CH2 <- 222 
	} () 

	A: = <- CH2 
	B: = <- CH2 
	fmt.Println (A, B) // 111 222 
	fmt.Println ( "end of program") 
}

 (5) can be used for range acquisition channel content

//Learn_Go/main.go
package main

import "fmt"

func main() {
	ch1 := make(chan int)
	ch2 := make(chan int)

	go func() {
		for i := 0; i<10;i++{
			ch1 <- i
		}
		ch2 <- 222
	}()

	go func() {
		for n := range ch1{
			fmt.Println(n)
		}
	}()
	<- ch2
	fmt.Println("程序结束")
}

10.5.select

select execution

  • Each case must be an IO operation
  • Which case which can be performed on the implementation of
  • All case can not execute, execute default
  • All the case can not be performed and there is no default, will block
//Learn_Go/main.go
package main

import "fmt"

func main() {
	ch1 := make(chan int,2)       //有缓存的channel
	ch2 := make(chan int,3)
	ch1 <- 111
	ch2 <- 222
	select {
	case a := <-ch1:
		fmt.Println(a)
	case b := <-ch2:
		fmt.Println(b)
	default:
		fmt.Println("错误")
	}
}

 Multi-loop and select for use in combination

//Learn_Go/main.go
package main

import "fmt"

func main() {
	ch := make(chan int)
	for i := 0; i < 10;i++{
		go func(j int) {
			ch <- j
		}(i)
	}
	//用for循环一直接受
	for {
		select {
		case a := <- ch:
			fmt.Println(a)
		default:
		}
	}
}

  

Guess you like

Origin www.cnblogs.com/derek1184405959/p/11333376.html