cancel method of timerCtx of context package

79 views
Skip to first unread message

qinggeer Bao

unread,
May 12, 2021, 9:43:16 PM5/12/21
to golang-nuts
In the cancel method of timerCtx type:

func (c *timerCtx) cancel(removeFromParent bool, err error) {
    c.cancelCtx.cancel(false, err)
    if removeFromParent {
        // Remove this timerCtx from its parent cancelCtx's children.
        removeChild(c.cancelCtx.Context, c)
    }
    c.mu.Lock()
    if c.timer != nil {
        c.timer.Stop()
        c.timer = nil
    }
    c.mu.Unlock()
}

why it use the following:
    c.cancelCtx.cancel(false, err)
    if removeFromParent {
        // Remove this timerCtx from its parent cancelCtx's children.
        removeChild(c.cancelCtx.Context, c)
    }

instead, direclty call 
c.cancelCtx.cancel(removeFromParent, err)

Is there any reason? 

Ian Lance Taylor

unread,
May 19, 2021, 11:31:10 AM5/19/21
to qinggeer Bao, golang-nuts
The two calls look the same but they are not the same. The c passed
to removeChild by cancelCtx.cancel is not the same as the c passed to
removeChild by timerCtx.cancel.

Ian
Reply all
Reply to author
Forward
0 new messages