douyan6958 2013-10-03 00:10
浏览 150
已采纳

如何在Golang中使用RWMutex?

type Stat struct {
    counters     map[string]*int64
    countersLock sync.RWMutex
    averages     map[string]*int64
    averagesLock sync.RWMutex
}

It is called below

func (s *Stat) Count(name string) {
    s.countersLock.RLock()
    counter := s.counters[name]
    s.countersLock.RUnlock()
    if counter != nil {
        atomic.AddInt64(counter, int64(1))
        return
    }
}

My understanding is that we first lock the receiver s (which is a type Stat) and then we add to it if the counter does exist.

Questions:

Q1: why do we need to lock it? What does RWMutex even mean?

Q2: s.countersLock.RLock() - does this lock up the entire receiver s or only the counters field in type Stat?

Q3: s.countersLock.RLock() - does this lock up the averages field?

Q4: Why should we use RWMutex? I thought channel was the preferred way to handle concurrency in Golang?

Q5: What is this atomic.AddInt64. Why do we need atomic in this case?

Q6: Why would we unlock right before we add to it?

  • 写回答

2条回答 默认 最新

  • doulu7921 2013-10-03 01:00
    关注

    Questions:

    Q1: why do we need to lock it? What does RWMutex even mean?

    RW stands for Read/Write. CF doc: http://golang.org/pkg/sync/#RWMutex.

    We need to lock it to prevent other routines/thread to change the value while we process it.

    Q2: s.countersLock.RLock() - does this lock up the entire receiver s or only the counters field in type Stat?

    As a mutex, the lock occurs only when you call the RLock() function. If any other goroutine already called the WLock(), then it blocks. You can call any number of RLock() within the same goroutine, it won't lock.

    So it does not lock any other fields, not even s.counters. In your example, you lock the map lookup to find the correct counter.

    Q3: s.countersLock.RLock() - does this lock up the averages field?

    No, as said in Q2, a RLock locks only himself.

    Q4: Why should we use RWMutex? I thought channel was the preferred way to handle concurrency in Golang?

    Channel is very useful but sometimes it is not enough and sometimes it does not make sense.

    Here, as you lock the map access, a mutex makes sense. With a chan, you'd have to have a buffered chan of 1, send before and receive after. Not very intuitive.

    Q5: What is this atomic.AddInt64. Why do we need atomic in this case?

    This function will increment the given variable in an atomic way. In your case, you have a race condition: counter is a pointer and the actual variable can be destroyed after the release of the lock and before the call to atomic.AddInt64. If you are not familiar with this kind of things, I'd advise you to stick with Mutexes and do all processing you need in between the lock/unlock.

    Q6: Why would we unlock right before we add to it?

    You should not.

    I don't know what you are trying to do, but here is a (simple) example: https://play.golang.org/p/cVFPB-05dw

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog