golang并发写入具有相同值的uint64变量?

Jun*_*Woo -1 concurrency go

type simpleTx struct {
    gas uint64
}

func (tx *simpleTx) UpdateGas() {
    tx.gas = 125
}

func TestUpdateGas(t *testing.T) {
    var wg sync.WaitGroup
    wg.Add(100)

    tx := &simpleTx{}
    for i:=0; i <100; i++ {
        go func(t *simpleTx)() {
            tx.UpdateGas()
            wg.Done()
        }(tx)
    }

    wg.Wait()
}
Run Code Online (Sandbox Code Playgroud)

使用-race选项运行时,上面的测试会打印出“警告:数据竞赛” 。golang中是否有任何类型可用于具有相同值的并发写入?我是否需要始终使用互斥锁或原子变量?

was*_*mup 5

是的,有许多 Go 习惯用法可以使用 - 以防止数据竞争 - 并且在没有适当同步的情况下,不应并发写入变量(或并发读取和写入):

  1. 对于您的特殊情况 - 写入相同的值。使用sync.Once
type simpleTx struct {
    sync.Once
    gas uint64
}
func (tx *simpleTx) UpdateGas() {
    tx.Do(func() { tx.gas = 125 })
}
Run Code Online (Sandbox Code Playgroud)
  1. 使用atomic.StoreUint64的原子写:
atomic.StoreUint64(&tx.gas, 125)
Run Code Online (Sandbox Code Playgroud)
  1. 使用sync.Mutex
type simpleTx struct {
    sync.Mutex
    gas uint64
}

func (tx *simpleTx) UpdateGas() {
    tx.Lock()
    tx.gas = 125
    tx.Unlock()
}
Run Code Online (Sandbox Code Playgroud)
  1. 使用频道:
type simpleTx struct {
    gas chan uint64
}
func (tx *simpleTx) UpdateGas() {
    select {
    case tx.gas <- 125:
    default:
    }
}
func TestUpdateGas(t *testing.T) {
    var wg sync.WaitGroup
    tx := &simpleTx{make(chan uint64, 1)}
    for i := 0; i < 100; i++ {
        wg.Add(1)
        go func(t *simpleTx) {
            tx.UpdateGas()
            wg.Done()
        }(tx)
    }
    wg.Wait()
}
Run Code Online (Sandbox Code Playgroud)