Communication channels used to coroutine

Channel: the type of pipe used to send data (the process responsible for coordinating communication between)
the FIFO channel.

Statement:
var identifier Chan DataType
of the initialization value of the channel is nil
all types can be used to channel air interface.
Reference type channel. We also use the make () function to allocate memory class to it.

var ch1 chan string
ch1 = make(chan string)
ch1 := make (chan string)

Communication operators:
<-
flow channel
ch <- int1
effluent
NUM: = <- CH
<- CH

Deadlock:
1.runtime main coroutine checks if that channel (unbuffered) main coroutine other end is not used, will be reported deadlock
2 buffer: write several data, but because there is another segment when the full channel can not continue to write, then deadlock.
2.main coroutine a coroutine wait to send data to it, while waiting coroutine main coroutine transmitting data to it, resulting in a deadlock.
2. In addition to the deadlock can not be avoided between the outside of the main coroutine coroutine, needs its own check.

Obstruction:
can not read or write data to the channel situation is blocked.
The case of writing: unbuffered: where the receiver is not ready
buffer: channel full time
where reading: unbuffered: Sender not ready situation
buffer: blank channel
using a buffered channel:
buf : 100 =
CHl: = the make (Chan String, buf)
using channel buffer: more scalable.

Semaphore mode:
coroutine write data to another coroutine, it indicates the end of execution (this data waiting for another coroutine coroutine)
FUNC A (A_B, B_a Chan int) {
for {
<- B_a
fmt.Println ( " A ")
A_B <-1
}

}
func B(A_B,B_A chan int){
for {
<-A_B
fmt.Println("B")
B_A<-1
}

}

Provided with anonymous function (coroutine) channel
Go FUNC () {
Cha_B <-1
} ()
with a buffer channel resource semaphore achieve
the same total number of channel capacity and the number of resource 1.
2. The length of the inner element and the currently used channel resource the same number
3. channel capacity minus the total number of elements equal to the length of the channel element unused
P, V operation
P operation of writing a plurality of elements into the passageway to indicate how many resources are occupied
V elements to a plurality of write operations to indicate the passage how many elements are released
lock latch (recommended to set a separate channel lock latch (buffer as. 1))
lock, filled element into the channel, the channel is filled, the other co-routines use the lock will block.
UNLOCK, all the elements in the release passage, so that other processes can continue their co-operation lock.
Read-write channel element is an atomic operation.


func P(n int){
e := new(empty.Empty)
for i:= 0;i<n ;i++ {
ch1<-*e
}
}
func V(n int){
for i:= 0 ;i <n ;i++ {
<-ch1
}
}
func wait(n int){
P(n)
}
func Siganl(n int){
V(n)
}

chLock := make(chan int,1)
func P(){ chLock<-1}
func V(){ <- chLcok}
func lock(){
P(1)
}
func unlock(){
V(1)
}

Channel for loop
for V: Range = CH {
...............
}
normal for (iteration)
for I: = 0; I <c.len (); I ++ {
CH < - c.items [I]
}

Commonly used in the direction of the channel :( coroutine parameter, so that the channel may be transmitted according to a fixed direction)
as it will appear in the function coroutine where a channel may be either write-read, but in most cases (lock is a special case)
our channels are directed transmission between two channels coroutine.
var send_only chan <- int (sending only)
var recv_only <- Chan int (receive-only, receive-only channel can not be closed, since it is useless shutdown function)

Channel is closed :( may also receive value)
Close (CH) it is actually not as marked by the passage <- reception value.
(Write data to a channel has been closed off and the second passage will lead to panic)
to test whether the channel is closed
v, ok: = <- ch ( read second return value is normal read, read already some elements returns true, but the channel is closed, finished reading all the elements that you can continue to read, then do not panic occurs, read a zero value and false)
(false appearance can be expressed as the channel is closed a.)

Nonblocking channel (can not be closed) reading, using the SELECT
SELECT {
Case U: = <- CHl:
...
Case V: = <- CH2:
...
...
default: // NO value to READY Received bE
...
}
. 1> are blocked if (no default), waits
2> can be processed if a plurality of randomly selected from a
3> can not handle, perform default
FUNC main () {
CHl: the make = (int Chan )
CH2: = the make (Chan int)
CH3: = the make (Chan int)
Go FUNC () {
for {
time.sleep (1000)
CHl <-. 1
CH2 <- 2
CH3 <-. 3
}

}()
for {
time.Sleep(100)
select {
case num := <-ch1:
fmt.Println(num)
case num := <- ch2:
fmt.Println(num)
case num := <- ch3:
fmt.Println(num)
default:
fmt.Println("no nimber")
}
}

Channel, time-out, the timer
time packet:
time.Ticker structure, at a specified interval time value is repeatedly transmitted to the channel C (internal fields).
Coroutine and recovery (Recover)
panicking independent and occurs in other coroutine coroutine. All processing is only in this coroutine

Using locks (sunc.lock) scenario:
1> to access shared data cache information structure
2> and save context state information that references the data
used channel scenarios:
1> to interact with the result of the asynchronous operation
2> distribution task
3> data transfer ownership

Guess you like

Origin www.cnblogs.com/mcmx/p/11390899.html