使用context实现一对多的goroutine协作
参考实现
func main() {
coordinateWithContext()
}
func coordinateWithContext() {
total := 12
var num int32
cxt, cancelFunc := context.WithCancel(context.Background())
for i := 1; i <= total; i++ {
go addNum(&num, i, func() {
if atomic.LoadInt32(&num) == int32(total) {
cancelFunc()
}
})
}
<-cxt.Done()
}
func addNum(numP *int32, id int, deferFunc func()) {
defer func() {
deferFunc()
}()
for i := 0; ; i++ {
currNum := atomic.LoadInt32(numP)
newNum := currNum + 1
time.Sleep(time.Millisecond * 200)
if atomic.CompareAndSwapInt32(numP, currNum, newNum) {
break
}
}
}
源码解析
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
- context.Background()返回的就是emptyCtx,代表根上下文,emptyCtx类型是int的别名类型,该类型的指针类型实现了Context接口,background具有唯一地址:
var (
background = new(emptyCtx)
)
func Background() Context {
return background
}
type emptyCtx int
func (*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
}
- context.WithCancel()做了如下几件事:
- 使用一个Context接口类型的变量创建一个cancelCtx类型变量
- 返回cancelCtx类型的指针值,还有一个匿名函数,这个匿名函数内部调用cancelCtx类型变量的cancel方法。
- 匿名函数第一个参数值true表示取消时将cancelCtx从Context的孩子中去掉,第二个参数表示调用cancelCtx.Err()看到的错误就是Canceled
var Canceled = errors.New("context canceled")
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
c := newCancelCtx(parent)
propagateCancel(parent, &c)
return &c, func() { c.cancel(true, Canceled) }
}
func newCancelCtx(parent Context) cancelCtx {
return cancelCtx{Context: parent}
}
type cancelCtx struct {
Context
mu sync.Mutex
done chan struct{}
children map[canceler]struct{}
err error
}
func (c *cancelCtx) Done() <-chan struct{} {
c.mu.Lock()
if c.done == nil {
c.done = make(chan struct{})
}
d := c.done
c.mu.Unlock()
return d
}
func (c *cancelCtx) Err() error {
c.mu.Lock()
defer c.mu.Unlock()
return c.err
}
var closedchan = make(chan struct{})
func init() {
close(closedchan)
}
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
}
c.err = err
if c.done == nil {
c.done = closedchan
} else {
close(c.done)
}
for child := range c.children {
child.cancel(false, err)
}
c.children = nil
c.mu.Unlock()
if removeFromParent {
removeChild(c.Context, c)
}
}
扩展
除了WithCancel可以繁衍Context,还有WithDeadline、WithTimeout、WithValue。
以上四个函数第一个参数都是Context类型。- WithTimeout是对WithDeadline的封装,WithDeadline第二个参数接收的是绝对时间,WithTimeout第二个参数接收的是相对时间。定时撤销的错误类型是context.DeadlineExceeded。
- 首次撤销会赋err值,关闭done通道,并且深度优先的撤销子Context,最终将本Context断开与父值的联系。(因为本Context内嵌的父值)
WithValue
- WithValue得到的Context值是不可以撤销的,在撤销信号传播时,若遇到会直接跨过。并且会将信号传递给子值。
- 只有valueCtx可以存储数据,所以遇到其他的Ctx比如cancelCtx会直接跳过。
- 没有修改数据的接口,如果存储的是引用类型,比如切片可以从外部修改,需要自己保证并发安全
func WithValue(parent Context, key, val interface{}) Context {
if key == nil {
panic("nil key")
}
if !reflect.TypeOf(key).Comparable() {
panic("key is not comparable")
}
return &valueCtx{parent, key, val}
}
type valueCtx struct {
Context
key, val interface{}
}
func (c *valueCtx) Value(key interface{}) interface{} {
if c.key == key {
return c.val
}
return c.Context.Value(key)
}