Golang并发控制WaitGroup和Context

sync.WaitGroup (package sync)

  •     func (wg *WaitGroup) Add(delta int)

  •     func (wg *WaitGroup) Done()

  •     func (wg *WaitGroup) Wait()

//@brief:  golang并发控制//@note:   WaitGroup和Context//@author: richardpackage mainimport ("fmt""sync")type httpPkg struct{}var  http httpPkgfunc (httpPkg) Get(url string) {  fmt.Println(url)}//WaitGroup//场景: A/B/C 异步执行,D需要在A/B/C都完成后才被执行//  A----->}//  B----->}--->D--->//  C----->}func main() {var gwg sync.WaitGroupvar urls = []string{    "http://1.org",    "http://2.org",    "http://3.org",  }  fmt.Println("all start")  for i := range urls {      gwg.Add(1)      go func(lwg *sync.WaitGroup, url string) {          defer lwg.Done()          http.Get(url)      }(&gwg, urls[i])  }// Wait for all HTTP fetches to complete.  gwg.Wait()  fmt.Println("all done")}///结果//all starthttp://3.orghttp://1.orghttp://2.orgall done

Context (package context)

  • func Background() Context

  • func TODO() Context

  • func WithValue(parent Context, key, val interface{}) Context

//@brief:  golang并发控制//@note:   WaitGroup和Context//@author: richardpackage mainimport (  "context"  "fmt"  "time")//Context 主动的通知goroutine执行操作//场景: A执行启动B/C/D协程, C启动了E/F协程,在某种条件下需要停止E/F。//      {B--->// A--->{C--->--->{E--->//      {         {F--->//      {D---> //func main() {  //@note: cancel  ctx, cancel := context.WithCancel(context.Background())  go test(ctx,"01")  go test(ctx,"02")  go test(ctx,"03")  time.Sleep(10 * time.Second)  cancel()  time.Sleep(10 * time.Second)}func test(ctx context.Context, name string) {  for {    select {    case <-ctx.Done():      fmt.Println(name,"exit")      return    default:      fmt.Println(name,"running")      time.Sleep(time.Second)    }  }}///结果//03 running02 running01 running03 running01 running02 running02 running01 running03 running01 exit03 exit02 exit

Context 使用原则

  • 不用Context放在结构体, 应该参数的方式传递

  • 不要什么都使用这个传递,Context的Value相关方法应传递数据,

  • Context作为第一参数 ctx

  • 不要传递nil。context.TODO

  • Context线程安全

sync.WaitGroup

  • goroutine间执行无序

  • [sync.WaitGroup 踩坑样本] 

    • https://liudanking.com/golang/golang-waitgroup-usage/ 

参考文档

  1. https://draveness.me/golang/concurrency/golang-context.html

  2. https://www.flysnow.org/2017/05/12/go-in-action-go-context.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cugriver

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值