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 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择