# golang之包和锁的机制

pyyu · · 2958 次点击 · · 开始浏览

# 互斥锁

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
for i := 0; i < 2; i++ {
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"
"time"
)

//读写锁
var rwLock sync.RWMutex

func testRWLock() {
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) {
rwLock.Lock()
b[8] = rand.Intn(100)
rwLock.Unlock()
}(a)
}
for i := 0; i < 100; i++ {
go func(b map[int]int) {
rwLock.RLock() //读锁
fmt.Println(a)
rwLock.RUnlock()
}(a)
}
time.Sleep(time.Second * 20)

}
func main() {

testRWLock()
//读多写少的时候，用读写锁
}

# 读写锁，互斥锁，性能比较

package main

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

//读写锁
var rwLock sync.RWMutex
var lock sync.Mutex

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.Microsecond) //微妙
//rwLock.Unlock()
lock.Unlock()

}(a)
}
for i := 0; i < 100; i++ {
go func(b map[int]int) {
for {
//rwLock.RLock() //读写锁的代码
lock.Lock()
time.Sleep(time.Millisecond)
//rwLock.RUnlock()
lock.Unlock()
}
}(a)
}
time.Sleep(time.Second * 20)
}
func main() {
//互斥锁
testRWLock()
//读多写少的时候，用读写锁
}

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

# 互斥锁

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
for i := 0; i < 2; i++ {
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"
"time"
)

//读写锁
var rwLock sync.RWMutex

func testRWLock() {
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) {
rwLock.Lock()
b[8] = rand.Intn(100)
rwLock.Unlock()
}(a)
}
for i := 0; i < 100; i++ {
go func(b map[int]int) {
rwLock.RLock() //读锁
fmt.Println(a)
rwLock.RUnlock()
}(a)
}
time.Sleep(time.Second * 20)

}
func main() {

testRWLock()
//读多写少的时候，用读写锁
}

# 读写锁，互斥锁，性能比较

package main

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

//读写锁
var rwLock sync.RWMutex
var lock sync.Mutex

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.Microsecond) //微妙
//rwLock.Unlock()
lock.Unlock()

}(a)
}
for i := 0; i < 100; i++ {
go func(b map[int]int) {
for {
//rwLock.RLock() //读写锁的代码
lock.Lock()
time.Sleep(time.Millisecond)
//rwLock.RUnlock()
lock.Unlock()