实例:
var mutex sync.Mutex //互斥锁
func printer(str string){
mutex.Lock() //加锁
defer mutex.Unlock() //解锁
for _,ch:=range str{
fmt.Printf("%c",ch)
time.Sleep(time.Millisecond*300)
}
}
func user1(){
printer("hello ")
}
func user2(){
printer("world")
}
func main() {
go user1()
go user2()
for {
;
}
}
说明:
var mutex sync.Mutex //声明一个互斥锁
mutex.Lock() //加锁
mutex.Unlock() //解锁