Go-互斥锁的那些事

对于互斥锁:

  • 不要重复锁
  • 没锁不要解锁
  • 不要赋值/函数间传递/函数返回
  • 锁定, 紧接着 defer 解锁

对于读写锁:

  • 读锁 + 读锁 不阻塞
  • 其他都阻塞

上代码, 自己品:

package main

import (
	"fmt"
	"sync"
	"time"
)

var i = 0

func count() int {
	i++
	return i
}

func main() {
	var locker sync.Mutex

	for i := 0; i < 10; i++ {
		go func(i int) {
			locker.Lock()
			defer locker.Unlock()

			fmt.Printf("%d: %d\n", i, count())
			time.Sleep(time.Millisecond * 200)
		}(i)
	}

	time.Sleep(time.Second * 5)

}
package main

import (
	"sync"
)

func main() {
	var locker sync.Mutex
	locker.Lock()
	locker.Lock() // fatal error: all goroutines are asleep - deadlock!
	defer locker.Unlock()

}
package main

import (
	"sync"
	"time"
)

func main() {
	var locker sync.Mutex

	go func() {
		locker.Lock()
		locker.Lock() // 阻塞在这里, 没有错误信息, 也没有 panic
		defer locker.Unlock()
	}()

	time.Sleep(time.Second * 5)
}
package main

import (
	"fmt"
	"sync"
)

func main() {
	var locker sync.Mutex

	locker.Unlock() // fatal error: sync: unlock of unlocked mutex
	fmt.Println("done")

}
package main

import (
	"fmt"
	"sync"
)

func main() {
	var locker sync.Mutex

	locker.Lock()
	locker.Unlock()
	locker.Unlock() // fatal error: sync: unlock of unlocked mutex
	fmt.Println("done")

}
package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	var locker sync.Mutex

	go func() {
		locker.Unlock() // fatal error: sync: unlock of unlocked mutex
		fmt.Println("done")
	}()

	time.Sleep(time.Second * 2)
}
package main

import (
	"fmt"
	"sync"
)

func main() {
	var locker sync.Mutex

	locker2 := locker // lock2 是 locker 的副本, 不一样了

	locker.Lock()
	locker2.Lock() // 没有阻塞
	defer locker2.Unlock()
	defer locker.Unlock()
	fmt.Println("done")

}
package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	var locker sync.Mutex

	go func() {
		locker.Lock()
		defer locker.Unlock()
		time.Sleep(time.Second * 2)
		fmt.Println("aaa")
	}()

	go func() {
		locker.Lock()
		defer locker.Unlock()
		time.Sleep(time.Second * 2)
		fmt.Println("bbb")
	}()

	time.Sleep(time.Second * 5)
}
package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	var locker sync.Mutex

	locker2 := locker

	go func() {
		locker.Lock()
		defer locker.Unlock()
		time.Sleep(time.Second * 2)
		fmt.Println("aaa")
	}()

	go func() {
		locker2.Lock()
		defer locker2.Unlock()
		time.Sleep(time.Second * 2)
		fmt.Println("bbb")
	}()

	time.Sleep(time.Second * 5)
}
package main

import (
	"sync"
)

func main() {
	var locker sync.RWMutex

	locker.Lock()
	locker.Lock() // fatal error: all goroutines are asleep - deadlock!

	defer locker.Unlock()
	defer locker.Unlock()

}
package main

import (
	"sync"
)

func main() {
	var locker sync.RWMutex

	locker.Lock()
	locker.RLock() // fatal error: all goroutines are asleep - deadlock!

	defer locker.Unlock()
	defer locker.RUnlock()

}
package main

import (
	"sync"
)

func main() {
	var locker sync.RWMutex

	locker.RLock()
	locker.Lock() // fatal error: all goroutines are asleep - deadlock!

	defer locker.RUnlock()
	defer locker.Unlock()

}
package main

import (
	"fmt"
	"sync"
)

func main() {
	var locker sync.RWMutex

	locker.RLock()
	locker.RLock()

	defer locker.RUnlock()
	defer locker.RUnlock()

	fmt.Println("done") // done
}
package main

import (
	"fmt"
	"sync"
)

func main() {
	var locker sync.RWMutex

	rlocker := locker.RLocker()

	rlocker.Lock()
	rlocker.Lock()

	defer rlocker.Unlock()
	defer rlocker.Unlock()

	fmt.Println("done") // done
}
上一篇:选择一个运行Derby工具和启动辅助程序的方法


下一篇:flutter问题收集_not sync podfile.lock