This is a creation in
Article, where the information may have evolved or changed.
When we write C, if we use multi-process, we usually use the signal, management and so on to the process of communication between processes, then golang how to achieve this?? Just look at the code and talk.
Package Mainimport ( "FMT" "Time") Func main () { timeout: = Make (chan bool, 1) go func () { &N Bsp Fmt. Println ("------------Sub-process 1--------------") T1: = time. Now (). Unixnano () FMT. PRINTLN (t1) FMT. Println ("This will certainly be done") time. Sleep (3 * time. Second) //timeout <-true timeout <-true } () FMT. PRINTLN ("First logical OR response main function") go func () { FMT. Println ("------------Sub-process 2--------------") t2: = time. Now (). Unixnano () FMT. Println (T2) FMT. Println ("Equivalent to fork a subprocess in progress") } () ch: = make (chan int) Select { Case <-ch:& nbsp Case <-timeout: FMT. Println ("------------back to main function--------------") FMT. PRINTLN ("task is timeout!") } FMT. PRINTLN ("Output of the main function itself")}
We execute the code and take a look at the results
首先逻辑还是响应 main 函数------------ 子进程2--------------1471577916141068800相当于fork一个子进程在进行------------ 子进程1--------------1471577916142068800这个一定会执行------------ 回到main函数 --------------task is timeout!main 函数本身的输出
From the time stamp of nanosecond, it can be seen that two go func () is executed almost simultaneously, this is the Golang parallel processing, is the direct call system Epoll processing flow, Golang also provides a monitoring method to monitor the return of each parallel processing logic, Select is the way