在sync / mutex.go中,“ _ =变量”是什么意思

时间:2019-01-22 08:55:45

标签: go underline

sync / mutex.go:

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

_ = m.state是什么意思?

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

强制读取m.state并打钩种族探测器。