Golang multi-coroutine asynchronous read and write demo

One, use two sync.WaitGroup and close the channel to achieve

package main

import (
	"fmt"
	"sync"
)

func main() {
    
    
	kk := make(chan int, 1000)
	var setwg sync.WaitGroup
	var readwg sync.WaitGroup
	for i := 1; i < 3; i++ {
    
    
		setwg.Add(1)
		go Set(kk, &setwg)
	}
	for i := 1; i < 3; i++ {
    
    
		readwg.Add(1)
		go Read(kk,&readwg)
	}
	setwg.Wait()


	close(kk)  //只要一个 chan 还有未读的数据,即使把它 close 掉,
	// 你还是可以继续把这些未读的数据消费完,而且bool值为true,之后才是读取零值数据和false
	readwg.Wait()

	//或者下面的实现也可以
	//for {
    
    
	//	if len(kk) == 0 {
    
    
	//		close(kk)
	//		readwg.Wait()
	//		break
	//	}
	//}
}
func Set(kk chan int, wg *sync.WaitGroup) {
    
    
	defer wg.Done()
	for i := 1; i < 3; i++ {
    
    
		kk <- i
	}
}

func Read(kk chan int,wg *sync.WaitGroup) {
    
    
	defer wg.Done()
	for {
    
    
		i, ok := <-kk
		if !ok{
    
    
			fmt.Println("ok:",ok)
			break
		}
		fmt.Println(ok)
		fmt.Println(i)
	}
}



Second, use two sync.WaitGroup and the cancel operation through context to achieve

package main

import (
	"context"
	"fmt"
	"sync"
)

func main() {
    
    
	kk := make(chan int, 1000)
	ctx, cancel := context.WithCancel(context.Background())
	var setwg sync.WaitGroup
	var readwg sync.WaitGroup
	for i := 1; i < 3; i++ {
    
    
		setwg.Add(1)
		go Set(kk, &setwg)
	}
	for i := 1; i < 3; i++ {
    
    
		readwg.Add(1)
		go Read(kk, ctx,&readwg)
	}
	setwg.Wait()
	for {
    
    
		if len(kk) == 0 {
    
    
			cancel()
			readwg.Wait()
			break
		}
	}
}
func Set(kk chan int, wg *sync.WaitGroup) {
    
    
	defer wg.Done()
	for i := 1; i < 3; i++ {
    
    
		kk <- i
	}
}

func Read(kk chan int, ctx context.Context,wg *sync.WaitGroup) {
    
    
	defer wg.Done()
	for {
    
    
		select {
    
    
		case <-ctx.Done():
			return
		default:
			if len(kk)>0{
    
    
				i:=<-kk
				fmt.Println(i)
			}
		}
	}
}

Third, use a sync.WaitGroup and the cancel operation through the context to determine the number of goruntines (not significant)

package main

import (
	"context"
	"fmt"
	"runtime"
	"sync"
)

func main() {
    
    
	kk := make(chan int, 1000)
	ctx, cancel := context.WithCancel(context.Background())
	var setwg sync.WaitGroup
	for i := 1; i < 3; i++ {
    
    
		setwg.Add(1)
		go Set(kk, &setwg)
	}
	for i := 1; i < 3; i++ {
    
    
		go Read(kk, ctx)
	}
	setwg.Wait()
	for {
    
    
		if len(kk) == 0 {
    
    
			cancel()
			break
		}
	}
	for {
    
    
		fmt.Println("runtime.NumGoroutine()",runtime.NumGoroutine())
		if runtime.NumGoroutine()==1 {
    
    
			break
		}
	}
}
func Set(kk chan int, wg *sync.WaitGroup) {
    
    
	defer wg.Done()
	for i := 1; i < 3; i++ {
    
    
		kk <- i
	}
}

func Read(kk chan int, ctx context.Context) {
    
    
	for {
    
    
		select {
    
    
		case <-ctx.Done():
			return
		default:
			if len(kk)>0{
    
    
				i:=<-kk
				fmt.Println(i)
			}
		}
	}
}

Guess you like

Origin blog.csdn.net/csdniter/article/details/109527870