duanmou9228 2016-05-02 19:16
浏览 270

Golang:互斥锁已保持太长时间后出现恐慌

I am trying to figure out what is that is making my program hanging, most of my locks shouldn't be held for more than 200 ms. (actually much less!)

I want to create two new functions (Lock() and Unlock()) so that Lock will have a timer that will panic if Lock has been held for more than 200 ms.

This is my current attempt, but it doesn't work, any hint?

type ShardKV struct {
  lockChan chan bool
}

func (kv *App) lock(reason string) {
    kv.mu.Lock()

    f := func () {
        fmt.Println("PANIC: ms passed")
        select {
        case <- kv.lockChan:
            //
        default:
            panic("PANIC: " + reason)
        }
  }
    time.AfterFunc(time.Second * 10, f)
}

func (kv *App) unlock(reason string) {
    kv.lockChan <- true
    kv.mu.Unlock()
}
  • 写回答

3条回答 默认 最新

  • drtoclr046994545 2016-05-02 20:59
    关注

    Your lock function waits for 10 seconds befor calling the f function which holds the select statement, a simpler way of achieving what you are trying to do might be this:

    func (kv *App) lock(reason string) {
        kv.mu.Lock()
        select {
        case <- kv.lockChan:
            //
        case <-time.After(time.Second * 10):
            panic("PANIC: " + reason)
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100