美文网首页
GO互斥锁、读写锁

GO互斥锁、读写锁

作者: 骑蜗上高速 | 来源:发表于2019-12-26 17:23 被阅读0次

读写锁:
读时共享,写时独占。写锁优先级比读锁优先级高

package main
import (
    "fmt"
    "sync"
    "time"
)
var mutex sync.Mutex //新建一个互斥锁,默认状态为0,未加锁
func printer1(str string){
    mutex.Lock() //访问共享数据之前,加锁
    for _,ch := range str {
        fmt.Printf("%c",ch)
        time.Sleep(time.Millisecond*300)
    }
    mutex.Unlock()//访问结束,解锁
}
func person11 () {
    printer1("Hello")
}
func person22 () {
    printer1("World")
}
func main() {
    go person11()
    go person22()

    for {
        ;
    }
}

通过mutex实现读时共享,写时独占
代码示例:

package main
import (
    "fmt"
    "math/rand"
    "sync"
    "time"
)
var index int
var rwMutxt1 sync.RWMutex
func readRand1(num int)  {
    for {
        rwMutxt1.RLock()
        num =  index
        fmt.Printf("########thread%d读,读出%d\n",num,index)
        rwMutxt1.RUnlock()
    }
}
func writeRand1(num int)  {
    for {
        randNum := rand.Intn(1000)
        rwMutxt1.Lock()
        index = randNum
        fmt.Printf("thread%d写,写入%d\n",num,randNum)
        time.Sleep(time.Microsecond*300)
        rwMutxt1.Unlock()
    }
}

func main()  {
    rand.Seed(time.Now().UnixNano())
    for i :=0; i < 2; i++ {
        go writeRand1(i)

    }
    for i :=0; i < 2; i++ {
        go readRand1(i)
    }
    //<- quit
    for {
        ;
    }
}

打印结果:

thread0写,写入742
########thread742读,读出742
########thread742读,读出742
thread1写,写入316
########thread316读,读出316
########thread316读,读出316
thread0写,写入729
########thread729读,读出729
########thread729读,读出729

通过channel实现不了读时共享,写时独占
代码示例:

package main

import (
    "fmt"
    "math/rand"
    "time"
)
var quit = make(chan bool)

func readRand(in <- chan int,num int)  {
    for {
        rrand := <- in
        fmt.Printf("########thread%d读,读出%d\n",num,rrand)
    }
}
func writeRand(out chan <- int,num int)  {
    for {
        randNum := rand.Intn(1000)
        out <- randNum
        fmt.Printf("thread%d写,写入%d\n",num,randNum)
        time.Sleep(time.Microsecond*300)
    }    
}

func main()  {
    ch := make(chan int)
    rand.Seed(time.Now().UnixNano())
    for i :=0; i < 5; i++ {
        go writeRand(ch,i)
    }
    for i :=0; i < 5; i++ {
        go readRand(ch,i)
    }
    //<- quit
    for {
        ;
    }
}    

打印结果:

thread3写,写入93
########thread4读,读出93
thread1写,写入604
thread2写,写入78
########thread3读,读出165
thread4写,写入165
########thread1读,读出604
########thread0读,读出78
thread0写,写入769
########thread2读,读出769

相关文章

  • Go 语言的锁

    Go 语言提供两类锁: 互斥锁(Mutex)和读写锁(RWMutex)。其中读写锁(RWMutex)是基于互斥锁(...

  • GO互斥锁、读写锁

    读写锁:读时共享,写时独占。写锁优先级比读锁优先级高 通过mutex实现读时共享,写时独占代码示例: 打印结果: ...

  • 可重入读写锁 ReentrantReadWriteLock

    读写锁分为读锁和写锁,多个线程获取读锁不互斥,读写锁、写写锁互斥。 输出

  • 读写锁和互斥锁 读写互斥锁,简称读写锁 mux sync.RWMutex Lock和Unlock分别对写锁进行锁定...

  • golang 基础(31) 锁

    在 go 语言中提供了并发编程模型和工具外,还提供传统的同步工具锁,包括互斥锁和读写锁有关互斥锁有些内容我们必须清...

  • CopyOnWrite思想

    读写锁的弊端 读写锁的思想是读读不互斥,读写互斥,写写互斥最大的问题,其实就在于写锁和读锁的互斥。假设写操作频率很...

  • Golang 锁的相关知识

    Golang锁分类:互斥锁(Mutex)、读写锁(RWMutex)。 互斥锁 在编写代码中引入了对象互斥锁的概念,...

  • 读写锁和互斥锁的区别

    同步 互斥锁 读写锁 区别

  • C++锁

    锁的种类 互斥锁、条件锁、自旋锁、读写锁、递归锁。 互斥锁 头文件: 类型:pthread_mutex_t, 函数...

  • go RWMutex源码解析

    RWMutex 基于go 1.13源码总的来说读写锁就是利用互斥锁和CAS维护2个关于读锁的变量以及runtime...

网友评论

      本文标题:GO互斥锁、读写锁

      本文链接:https://www.haomeiwen.com/subject/fodloctx.html