Golang 筆記
  • Introduction
  • 安裝Golang
    • 本地編譯與安裝第三方套件
    • 第一個 GO 程式
    • export 與 import
    • 使用 go mod
  • 基本工具
  • DataBase操作
    • MySQL
    • 使用 ORM
    • MongoDB
  • 基本語法
    • Variable
    • BSON
    • JSON
    • 時間相關
    • Interface
    • Error
    • 型別
    • 字串
    • defer
    • panic, recover
    • Channel 與Goroutine
      • 讀寫鎖
      • for select 與 for range
      • UnBuffered channel 與 Buffered channel
    • Function
    • pointer
    • regExp
    • fmt
    • Make vs New
    • struct
    • Array, Slice 陣列
    • map
  • 核心模組
    • Reflect
    • File
    • Signal
    • BuiltIn
    • Sync
    • IO
    • Sort
    • http
    • crypto
    • context
  • 第三方模組
    • Dom Parser
    • gin 框架
    • Websocket
    • Iris框架
      • 讀取 Body 資料
      • 相關範例
    • Fiber 框架
    • 自動重啟 server 工具
    • go-jwt
  • 測試
  • 原始碼解析
  • 常見問題
    • 資料存取不正確
    • Data races
Powered by GitBook
On this page
  • 範例
  • 解決方法有三個

Was this helpful?

  1. 常見問題

Data races

Previous資料存取不正確

Last updated 4 years ago

Was this helpful?

假設我們有多個 CPU 同時執行程式,在使用 goroutine 後就容易出現此問題。

原因是多個 CPU 對相同記憶體進行操作最後導致資料不一致所產生。

詳細可見:

範例

以下程式每次執行的結果都不同。

原因是後面的 goroutine 照理說應該要拿前面加好的數字再往上加,但因為concurrency的關西,前面一個拿完但來不及算好且寫回變數,這時舊的變數就被下一個goroutine拿走要進行加一,於是產生最後加起來數字變少的情況。

package main

import (
	"fmt"
	"sync"
)

var total int
var wg sync.WaitGroup

// Inc increments the counter for the given key.
func inc(num int) {
	total += num
	wg.Done()
}

// Value returns the current value of the counter for the given key.
func getValue() int {
	return total
}

func main() {
	for i := 1; i <= 1000; i++ {
		wg.Add(1)
		go inc(i)
	}
	wg.Wait()
	fmt.Println(getValue())
}

可以試著加上 --race 來偵測。

執行: go run --race app.go

解決方法有三個

1.runtime.GOMAXPROCS(1)

設定 CPU 只用一顆執行程式

2.使用 sync lock 與 unlock

package main

import (
	"fmt"
	"sync"
)

var total int
var wg sync.WaitGroup
var mu sync.Mutex

// Inc increments the counter for the given key.
func inc(num int) {
	mu.Lock()
	defer mu.Unlock()
	total += num
	wg.Done()
}

// Value returns the current value of the counter for the given key.
func getValue() int {
	return total
}

func main() {
	for i := 1; i <= 1000; i++ {
		wg.Add(1)
		go inc(i)
	}
	wg.Wait()
	fmt.Println(getValue())
}

3.使用 sync/atomic

package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

var total uint64
var wg sync.WaitGroup

func inc(num uint64) {
	atomic.AddUint64(&total, num)
	wg.Done()
}

// Value returns the current value of the counter for the given key.
func getValue() uint64 {
	return atomic.LoadUint64(&total)
}

func main() {
	for i := uint64(1); i <= 1000; i++ {
		wg.Add(1)
		go inc(i)
	}
	wg.Wait()
	fmt.Println(getValue())
}

Javascript 不會有 race condition 的原因是其為 single thread ,並且 worker thread 也不能直接存取到相同變數,都要透過 postMessage 溝通。

https://stackoverflow.com/questions/21467325/javascript-thread-handling-and-race-conditions
https://golang.org/ref/mem
Data Race Detector - The Go Programming Language
Logo