Golang Parallel Operations and Time/sleep.go

Source: Internet
Author: User

Today I probably read a bit about the parallel operation of Golang, which briefly summarizes the parallel operation

go func()  // 可以启动一个协程

Multiple concurrent processes can be run at the same time, communication between the processes using channel (channels) for inter-process communication

The channel is defined as follows

c := chan string

means that this is a string type of channel

Channel can be understood as a queue, advanced first out, but it can only occupy the position of an element , we can be defined as follows to add/read elements, <-is an operator

c <- "hello"  //把"hello"加入到c通道中msg := <- c // 将c中的"hello"取出到msg里

In parallel operations we may need to read messages from multiple channels, at which point we use

for{  select{    case msg1 := channel_1:      fmt.Println(msg1)    case msg2 := channel_2:      fmt.Println(msg2)  }}

At this point, if Channel_1 and channel_2 two channels have no messages, then the process will be blocked in the SELECT statement until Channel_1 or channel_2 has a message

How to solve the problem of blocking?

We need to add one more case in select, the code is as follows

for{  select{    case msg1 := <- channel_1:      fmt.Println(msg1)    case msg2 := <- channel_2:      fmt.Println(msg2)    case msg3 := <- time.After(time.second*5):      fmt.Println("get message time out, check again...")  }}

This means that if you stay in select for 5 seconds and do not receive CHANNEL1 and CHANNEL2 messages, then a channel with the time.After(time.second*5) current time will be returned, and select will respond correctly

You see, the bloggers are a little stuck here.

Don't quite understand time. After () This is a thing, incredibly able to wait for 5 seconds to return to the channel
So helpless under can only look at time. After the source code, because is the first day to learn the go language so many things still do not understand, can only read the approximate

This is probably the case:

 //function prototype//file:. /time/sleep.gofunc after (d Duration) <-chan time {return Newtimer (d).            C}func Newtimer (d Duration) *timer {c: = Make (chan time, 1) T: = &timer{c:c, r:runtimetimer{ When:when (d), F:sendtime, Arg:c,},} Starttimer (&T.R) return T }type runtimetimer struct {tb uintptr i int when int64 period int64 F func (interface{}, UIntPtr)/ /Note:must not being closure arg interface{} seq uintptr}  

The above is related to time. The function prototype of after (), time. The following () steps are performed like this:

  1. After () call Newtimer () directly, create a timer object in Newtimer and store the pointer in T, and pass Starttimer (&T.R) into the Runtimetimer to create a co-process, This association sends a time object into the T.C channel after a specified period (5 seconds). (Runtimetimer does not delve into it here)
  2. Return t containing C (channel) and R (Runtimetimer) to Newtimer (d) of the After () function
  3. The After function returns Newtimer (d). C, the channel to which after () is eventually returned
  4. Select has passed for 5 seconds, and detects the return of the channel carrying the time object (current), then outputs "get message times out, check again ..."

This article is pending update .... Beginner Golang, big guy passing please correct me!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.