1. 程式人生 > >go 包-鎖機制

go 包-鎖機制

tom main 讀寫鎖 tex package 同步 pan bsp uil

線程同步

  • import(“sync”)
  • 互斥鎖, var mu sync.Mutex
  • 讀寫鎖, var mu sync.RWMutex

資源競爭樣例

func testMap() {
	var a map[int]int
	a = make(map[int]int, 5)

	a[8] = 10
	a[3] = 10
	a[2] = 10
	a[1] = 10
	a[18] = 10

	for i := 0; i < 2; i++ {
		go func(b map[int]int) {
			b[8] = rand.Intn(100)
		}(a)
	}

	fmt.Println(a)
}

編譯:go build -race go_dev\main

  • -race 資源競爭檢測

執行程序會拋資源競爭錯誤

引入互斥鎖解決上面的問題

package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

var lock sync.Mutex

func testMap() {
	var a map[int]int
	a = make(map[int]int, 5)

	a[8] = 10
	a[3] = 10
	a[2] = 10
	a[1] = 10
	a[18] = 10

	for i := 0; i < 2; i++ {
		go func(b map[int]int) {
			lock.Lock()
			b[8] = rand.Intn(100)
			lock.Unlock()
		}(a)
	}

	lock.Lock()
	fmt.Println(a)
	lock.Unlock()

	time.Sleep(time.Second)
}

func main() {
	testMap()
}

讀寫鎖+原子操作

package main

import (
	"fmt"
	"math/rand"
	"sync"
	"sync/atomic"
	"time"
)

var lock sync.Mutex
var rwLock sync.RWMutex

func testRWLock() {
	var a map[int]int
	a = make(map[int]int, 5)
	var count int32
	a[8] = 10
	a[3] = 10
	a[2] = 10
	a[1] = 10
	a[18] = 10

	for i := 0; i < 2; i++ {
		go func(b map[int]int) {
			rwLock.Lock()
			//lock.Lock()
			b[8] = rand.Intn(100)
			time.Sleep(10 * time.Millisecond)
			//lock.Unlock()
			rwLock.Unlock()
		}(a)
	}

	for i := 0; i < 100; i++ {
		go func(b map[int]int) {
			for {
				//lock.Lock()
				rwLock.RLock()
				time.Sleep(time.Millisecond)
				//fmt.Println(a)
				rwLock.RUnlock()
				//lock.Unlock()
				atomic.AddInt32(&count, 1)
			}
		}(a)
	}
	time.Sleep(time.Second * 3)
	fmt.Println(atomic.LoadInt32(&count))
}

func main() {
	testRWLock()
}

go 包-鎖機制