标签:上下文 exit 影响 return add break 结束 wait 执行
并发:逻辑上具备同时处理多个任务的能力(单核,上下文切换)
并行:物理上同一时刻执行多个并发任务(多核,互不影响)
package main import ( "fmt" "time" ) var c int func counter() int { c++ return c } func main() { a := 100 go func(x, y int) { time.Sleep(time.Second) //goroutine在main逻辑之后执行 fmt.Println("go:", x, y) }(a, counter()) //立即计算并赋值 a += 100 fmt.Println("main", a, counter()) time.Sleep(time.Second * 3) //等goroutine结束 }
进程退出并不会等并发任务执行结束,可用channel阻塞,然后发出退出信号
package main import ( "fmt" "time" ) func main() { exit := make(chan struct{}) go func() { time.Sleep(time.Second) fmt.Println("goroutine done") close(exit) //关闭通道,发出信号 }() fmt.Println("main start...") <-exit //通道关闭,立即解除阻塞 fmt.Println("main end...") }
等待多个任务结束,使用sync.WaitGroup,通过设定计数器,让每个goroutine在退出前递减,直至归零时解除阻塞
package main import ( "fmt" "sync" "time" ) func main() { var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) //累加计数 go func(id int) { defer wg.Done() //递归计数 time.Sleep(time.Second) fmt.Println("goroutine", id) }(i) } fmt.Println("start") wg.Wait() //阻塞,直至计数归零 fmt.Println("stop") }
GOMAXPROCS
与逻辑核数相等
package main import ( "fmt" "math" "runtime" "sync" ) func count() { x := 0 for i := 0; i < math.MaxUint32; i++ { x += i } fmt.Println(x) } func test(n int) { for i := 0; i < n; i++ { count() } } func test2(n int) { var wg sync.WaitGroup wg.Add(n) for i := 0; i < n; i++ { go func() { count() wg.Done() }() } wg.Wait() } func main() { n := runtime.GOMAXPROCS(0) test2(n) }
可在多处使用Wait阻塞,它们都能收到通知
package main import ( "fmt" "sync" "time" ) func main() { var wg sync.WaitGroup wg.Add(1) go func() { wg.Wait() fmt.Println("wait exit") }() go func() { time.Sleep(time.Second) fmt.Println("done") wg.Done() }() wg.Wait() fmt.Println("main exit") }
var 变量名 chan 类型
var test chan int
var test chan map[string]string
channel关闭
package main import ( "fmt" ) func main() { ch := make(chan int, 10) for i := 0; i < 10; i++ { ch <- i } close(ch) for { b, ok := <-ch if ok == false { break } fmt.Println(b) } }
channel读写
package main import ( "fmt" "time" ) func write(ch chan int) { for i := 0; i < 100; i++ { ch <- i } } func read(ch chan int) { var a int for { a = <-ch fmt.Println("read", a) } } func main() { intChan := make(chan int, 10) go write(intChan) go read(intChan) time.Sleep(time.Second) }
标签:上下文 exit 影响 return add break 结束 wait 执行
原文地址:https://www.cnblogs.com/hongpeng0209/p/9108048.html