【问题标题】:What is “_ = variable” mean in sync/mutex.gosync/mutex.go 中的“_ = variable”是什么意思
【发布时间】:2019-06-15 16:49:28
【问题描述】:

同步/mutex.go:

func (m *Mutex) Unlock() {
    if race.Enabled {
         _ = m.state
         race.Release(unsafe.Pointer(m))
     }
...

_ = m.state 是什么意思?

我知道var _ interface = Object 的意思是检查Object 是否实现了接口。

【问题讨论】:

  • 它强制读取 m.state。
  • 为什么读取 m.state 但不使用它?
  • 让比赛检测器工作。

标签: go underline


【解决方案1】:

从提交日志来看,原因是“_ = m.state”确保m不为nil。

commit 5bb3a66a973ea87494b9197091e8c1f122080627
Author: Rémy Oudompheng <oudomphe@phare.normalesup.org>
Date:   Mon Apr 8 23:46:54 2013 +0200

    sync, sync/atomic: do not corrupt race detector after a nil dereference.

    The race detector uses a global lock to analyze atomic
    operations. A panic in the middle of the code leaves the
    lock acquired.

    Similarly, the sync package may leave the race detectro
    inconsistent when methods are called on nil pointers.

    R=golang-dev, r, minux.ma, dvyukov, rsc, adg
    CC=golang-dev
    https://golang.org/cl/7981043

【讨论】:

    【解决方案2】:

    强制读取 m.state 并勾选竞争检测器。

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 2015-06-25
      • 2011-08-18
      • 1970-01-01
      • 2018-11-11
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多