Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用channel比mutex性能高? 你测试过吗? #134

Closed
xuesongbj opened this issue Jun 4, 2019 · 0 comments
Closed

使用channel比mutex性能高? 你测试过吗? #134

xuesongbj opened this issue Jun 4, 2019 · 0 comments

Comments

@xuesongbj
Copy link

@xuesongbj xuesongbj commented Jun 4, 2019

要想解决数据竞争的问题可以使用互斥锁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

@xuesongbj xuesongbj closed this Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.