Go buffered channel

 

Buffered channel

The syntax structure: cap to capacity

ch := make(chan type, cap)
  • Support channel buffer len () and cap ().
  • Only send data within the buffer capacity of the channel.
  • You can only receive data buffer within the channel length.
  • Capacity of the buffer channel refers to the number of channels can be stored value. We will specify the capacity of the buffer size when creating a channel using the make function.
  • The length of the buffer channel is the number of elements currently queued channel.

Channel is asynchronous, is created when a was opened up or channel capable of storing a plurality of values.
This type does not require simultaneous transmission and reception. As long as there is unused space in the buffer used to send data, or further comprising data may be received, then the communication will be performed without blocking. Only there is no value in the channel to be received, the reception operation will be blocked.

Examples of a

package main
import (
	"fmt"
)
func main() {
	//创建一个容量为3的缓冲信道
	ch := make(chan string, 3)
	ch <- "naveen"
	ch <- "paul"
	fmt.Println("capacity is", cap(ch))   //capacity is 3
	fmt.Println("length is", len(ch))     //length is 2
	fmt.Println("read value", <-ch)       //read value naveen
	fmt.Println("new length is", len(ch)) //new length is 1
}

   

Example Two

package main
import (
	"fmt"
	"time"
)
func write(ch chan int) {
	for i := 0; i < 5; i++ {
		ch <- i
		fmt.Println("successfully wrote", i, "to ch")
	}
	close(ch)
}

func main() {
	ch := make(chan int, 2)
	go write(ch)
	time.Sleep(2 * time.Second)
	for v := range ch {
		fmt.Println("read value", v,"from ch")
		time.Sleep(2 * time.Second)
	}
} 

Create a capacity of two channels.
coroutine Xianxiang write channel data is written to both hang then blocked, and print, the main coroutine sleep two seconds.
After two seconds, the main channel from a coroutine read data, and sleep two seconds, then continue to write a write data coroutine hang forward channel, and then blocked waiting for the master to read data path co-two seconds.


Deadlock

package main
import (
	"fmt"
)
func main() {
	ch := make(chan string, 2)
	ch <- "naveen"
	ch <- "paul"
	ch <- "steve"
	fmt.Println(<-ch)
	fmt.Println(<-ch)
}

When we write data to the channel, beyond the capacity of the channel, thus blocking the write occurs. Now you want to write to be able to proceed, there must be other co-routines to read the data channel.
However, in the program, and no concurrent coroutine to read the channel, so here deadlock (deadlock) occurs.

 

WaitGroup

Suppose we have three concurrent execution coroutine Go (Go from the main thread to create Association). Go main coroutine need to wait for these three coroutines after the execution will terminate. This can be achieved by WaitGroup.

main Package 

Import ( 
    "FMT" 
    "Sync" 
    "Time" 
) 

FUNC Process (I int, WG * sync.WaitGroup) { 
    fmt.Println ( "Started Goroutines", I) 
    time.sleep (2 * time.Second) 
    FMT. printf ( "% D Goroutines ended \ n-", I) 
    // a method for reducing the value of the Done WaitGroup counter, the thread should be executed last. 
    wg.Done () 
} 

/ * 
    WaitGroup for waiting for the end of a group of threads. 
    Parent thread calls the Add method to set the number of threads should wait. 
    Each thread is waiting for the call Done method should be ended. 
    Meanwhile, the main thread can call the Wait method blocks until the end of all threads. 
* / 
FUNC main () { 
    NO:. 3 = 
    var WG sync.WaitGroup 
    // concurrent coroutine 
    for I: = 0; I <NO; I ++ { 
        / *
            If the internal counter reaches 0, Wait method blocks waiting for the release of all threads, 
            if the counter is less than 0, the method panic. 
        * / 
        Wg.Add (. 1) 
        Go Process (I, & WG) 
    } 
    // WaitGroup the Wait method blocks until the counter is decremented to zero. 
    wg.Wait () 
    fmt.Println ( "over") 
}

 

Guess you like

Origin www.cnblogs.com/-wenli/p/11824921.html