Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up使用channel比mutex性能高? 你测试过吗? #134
Closed
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
要想解决数据竞争的问题可以使用互斥锁sync.Mutex,解决数据竞争(Data race),也可以使用管道解决,使用管道的效率要比互斥锁高.
package main
import "sync"
var mutex = sync.Mutex{}
var ch = make(chan bool, 1)
func UseMutex() {
mutex.Lock()
mutex.Unlock()
}
func UseChan() {
ch <- true
<-ch
}
BenchmarkUseMutex-4 100000000 20.5 ns/op
BenchmarkUseChan-4 20000000 62.6 ns/op