当前位置: 首页 > news >正文

网站大事记时间轴折叠wordpress 还原主题

网站大事记时间轴折叠,wordpress 还原主题,wordpress wnmp,工作空窗期怎么缴纳社保作用:用于在go协程中 传递上下文、超时、取消、传值 底层实现:是由互斥锁、channel、map来实现的 互斥锁:保护临界资源 channel: 用于信号通知,比如ctx.Done() map: 保存父ctx下派生的所有子ctx, 父ctx关闭,子ctx都关…

作用:用于在go协程中 传递上下文、超时、取消、传值

底层实现:是由互斥锁、channel、map来实现的
互斥锁:保护临界资源
channel: 用于信号通知,比如ctx.Done()
map: 保存父ctx下派生的所有子ctx, 父ctx关闭,子ctx都关闭

实现的接口

type Context interface {Deadline() (deadline time.Time, ok bool)Done() <-chan struct{}Err() errorValue(key interface{}) interface{}
}

空ctx

type emptyCtx intfunc (*emptyCtx) Deadline() (deadline time.Time, ok bool) {return
}func (*emptyCtx) Done() <-chan struct{} {return nil
}func (*emptyCtx) Err() error {return nil
}func (*emptyCtx) Value(key interface{}) interface{} {return nil
}

cancel ctx

使用map保存所有子ctx,确保父ctx cancel后,子ctx也cancel

type cancelCtx struct {Contextmu       sync.Mutex            // protects following fieldsdone     chan struct{}         // created lazily, closed by first cancel callchildren map[canceler]struct{} // set to nil by the first cancel callerr      error                 // set to non-nil by the first cancel call
}func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {if parent == nil {panic("cannot create context from nil parent")}c := newCancelCtx(parent)propagateCancel(parent, &c)return &c, func() { c.cancel(true, Canceled) }
}// newCancelCtx returns an initialized cancelCtx.
func newCancelCtx(parent Context) cancelCtx {return cancelCtx{Context: parent}
}func propagateCancel(parent Context, child canceler) {fmt.Println("propagateCancel")done := parent.Done()if done == nil {return // parent is never canceled}select {case <-done:// parent is already canceledchild.cancel(false, parent.Err())returndefault:}if p, ok := parentCancelCtx(parent); ok {p.mu.Lock()if p.err != nil {// parent has already been canceledchild.cancel(false, p.err)} else {if p.children == nil {p.children = make(map[canceler]struct{})}// 保存子ctxp.children[child] = struct{}{}}p.mu.Unlock()} else {atomic.AddInt32(&goroutines, +1)go func() {select {case <-parent.Done():child.cancel(false, parent.Err())case <-child.Done():}}()}
}func (c *cancelCtx) cancel(removeFromParent bool, err error) {if err == nil {panic("context: internal error: missing cancel error")}c.mu.Lock()if c.err != nil {c.mu.Unlock()return // already canceled}c.err = errif c.done == nil {c.done = closedchan} else {close(c.done) // 关闭channel, 用于通知ctx.Done()}// 关闭所有子ctxfor child := range c.children {// NOTE: acquiring the child's lock while holding parent's lock.child.cancel(false, err)}c.children = nilc.mu.Unlock()if removeFromParent {removeChild(c.Context, c)}
}

timeout ctx

在cancelctx的基础上实现,只是多了个定时器自动调用cancel

type timerCtx struct {cancelCtxtimer *time.Timer // Under cancelCtx.mu.deadline time.Time
}func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {if parent == nil {panic("cannot create context from nil parent")}if cur, ok := parent.Deadline(); ok && cur.Before(d) {// The current deadline is already sooner than the new one.return WithCancel(parent)}c := &timerCtx{cancelCtx: newCancelCtx(parent),deadline:  d,}propagateCancel(parent, c)dur := time.Until(d)if dur <= 0 {c.cancel(true, DeadlineExceeded) // deadline has already passedreturn c, func() { c.cancel(false, Canceled) }}c.mu.Lock()defer c.mu.Unlock()if c.err == nil {c.timer = time.AfterFunc(dur, func() {c.cancel(true, DeadlineExceeded)})}return c, func() { c.cancel(true, Canceled) }
}

value ctx

type valueCtx struct {Contextkey, val interface{}
}
func WithValue(parent Context, key, val interface{}) Context {if parent == nil {panic("cannot create context from nil parent")}if key == nil {panic("nil key")}//	if !reflectlite.TypeOf(key).Comparable() {//	panic("key is not comparable")//}return &valueCtx{parent, key, val}
}
func (c *valueCtx) Value(key interface{}) interface{} {if c.key == key {return c.val}return c.Context.Value(key)
}
http://www.yayakq.cn/news/778738/

相关文章:

  • 2015年做哪些网站致富WordPress中文改英文版
  • 新型网站建设电商平台系统
  • 网站建设全部代码网课平台搭建
  • 请私人做网站风险店面设计流程
  • 温州网站建设华一辽宁工程技术大学电子信息网
  • 校园网站建设价格电子商务网站建设 教案
  • 文化局网站建设方案专业企业网站建设公司价格
  • 网站备案 接入商客户制作网站时的问题
  • 能够做外贸的网站有哪些问题营销策略都有哪些方面
  • 北京网站优化实战p2c网站方案
  • 网站后台收入怎么做会计分录开发网站需要注意
  • wordpress建站详细教程视频江苏市场监督管理局app
  • 外贸网站经典营销案例剑网三奇遇查询网站怎么做
  • 上海网站公竞争对手网站分析
  • 国外游戏ui设计网站南阳建网站公司
  • 网站建设方案文档wordpress免费主题插件下载地址
  • 哪个网站可以做头像wordpress改浏览数数据库
  • 网站建设数据库模板ih5 wordpress
  • 新乡手机网站建设哪家好哪些网站可以做爬虫实验
  • 锦州如何做百度的网站免费下载app软件官网
  • 做筹款的网站需要什么资质网站手绘教程
  • 易企建站个人备案网站内容
  • 天津网站建设服务电话苏宁网站优化与推广
  • 天津市建设厅注册中心网站企业官方网站怎么申请
  • 七星彩网站建设网站建设与维护制作网页
  • 0资本建设网站长春seo推广
  • 一流的网站建设哪家好所有网站302跳转百度
  • 凡科怎么建站教程配送货wordpress
  • 自己做个网站怎么赚钱wordpress 视频站模版
  • 广告营销策划是做什么的东莞搜索seo关键词