dp152153 2015-08-13 08:36
浏览 334
已采纳

如何在goroutine闭包内更改外部变量的值

func (this *l) PostUpload(ctx *Context) {

    //ctx.Response.Status = 500

    l, err := models.NewL(this.Config)
    go func() {
        err = l.Save(file) 
        if err != nil {
            ctx.Response.Status = 500
            ctx.Response.Body = err
        } else {
            ctx.Response.Status = 204
        }
    }()
}

How to change ctx.Response.Status value inside the goroutine closure?

  • 写回答

1条回答 默认 最新

  • dqly83915 2015-08-13 09:19
    关注

    You have no guarantee to observe changes made to the value of a variable in another goroutine without synchronization. See The Go Memory Model for details.

    So if you want to change ctx.Response.Status in another goroutine, for this change to be guaranteed to be visible in the caller goroutine use synchronization.

    There are multiple synchronization primitives. You can use channels or the sync package.

    Using channels:

    ch := make(chan int)
    
    go func() {
        err = l.Save(file) 
        if err != nil {
            ctx.Response.Status = 500
            ctx.Response.Body = err
        } else {
            ctx.Response.Status = 204
        }
        ch <- 0 // Signal that ctx is updated
        // goroutine may do other works (not related to changing ctx)
    }()
    
    <- ch // Wait for the goroutine to finish updating ctx
    

    Using sync.WaitGroup:

    var wg sync.WaitGroup
    wg.Add(1)
    
    go func() {
        err = l.Save(file) 
        if err != nil {
            ctx.Response.Status = 500
            ctx.Response.Body = err
        } else {
            ctx.Response.Status = 204
        }
        wg.Done() // Signal that ctx is updated
        // goroutine may do other works (not related to changing ctx)
    }()
    
    wg.Wait() // Wait for the goroutine to finish updating ctx
    

    Using sync.Mutex:

    m := sync.Mutex{}
    m.Lock()
    
    go func() {
        err = l.Save(file) 
        if err != nil {
            ctx.Response.Status = 500
            ctx.Response.Body = err
        } else {
            ctx.Response.Status = 204
        }
        m.Unlock() // Signal that ctx is updated
        // goroutine may do other works (not related to changing ctx)
    }()
    
    m.Lock() // Wait for the goroutine to finish updating ctx
    

    Note:

    It is good practice to signal the completion (ctx update in your case) using defer so that if the started goroutine would end in some unexpected way (e.g. runtime panic), the caller goroutine would not get blocked forever. Note that however in this case the completion signal will only be sent at the end of the anonymous function (that's when deferred functions are executed).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波