当前位置: 首页 > news >正文

宣传页模板武汉外包seo公司

宣传页模板,武汉外包seo公司,网站二级页面做哪些东西,wdcp 网站建设go读写锁的实现原理 1、RWMutex读写锁的概念 读写锁也就是我们所使用的RWMutex,其实是对于go本身的mutex做的一个拓展,当一个goroutine获得了读锁后,其他goroutine同样可以获得读锁,但是不能获得写锁。相反,当一个go…

go读写锁的实现原理

1、RWMutex读写锁的概念

读写锁也就是我们所使用的RWMutex,其实是对于go本身的mutex做的一个拓展,当一个goroutine获得了读锁后,其他goroutine同样可以获得读锁,但是不能获得写锁。相反,当一个goroutine获得了写锁,其他goroutine既不能读也不能写,互斥的概念。

2、使用场景

适用于读多写少的情况

3、底层实现

读写锁实现的结构体位于src下的sync包下的rwmutex.go文件中

type RWMutex struct {w           Mutex  // held if there are pending writerswriterSem   uint32 // semaphore for writers to wait for completing readersreaderSem   uint32 // semaphore for readers to wait for completing writersreaderCount int32  // number of pending readersreaderWait  int32  // number of departing readers
}	

w字段代表着复用了互斥锁

writerSem代表写信号量,用于写等待读

readerSem代表读信号量,用于读等待写

readerCount代表当前执行读的goroutine数量

readerWait代表被阻塞的准备读的goroutine的数量

4、读锁的实现

加读锁

func (rw *RWMutex) RLock() {if atomic.AddInt32(&rw.readerCount, 1) < 0 {// A writer is pending, wait for it.runtime_SemacquireMutex(&rw.readerSem, false, 0)}
}

首先看这个if方法,为什么要判断小于0呢?

atomic.AddInt32(&rw.readerCount, 1) < 0调用的这个原子方法,目的就是当goroutine加读锁的时候读锁数量+1,如果返回的数量是负数,那么就代表了当前有其他写锁,这个时候就掉用runtime_SemacquireMutex方法休眠当前goroutine,readerSem就记录者这个goroutine。所以要判断是否小于0

释放读锁

// RUnlock undoes a single RLock call;
// it does not affect other simultaneous readers.
// It is a run-time error if rw is not locked for reading
// on entry to RUnlock.
func (rw *RWMutex) RUnlock() {if r := atomic.AddInt32(&rw.readerCount, -1); r < 0 {// Outlined slow-path to allow the fast-path to be inlinedrw.rUnlockSlow(r)}
}

释放读锁的时候就是对readerCount读数量-1即可,如果返回值小于0,就代表着当前有写的操作,这个时候就会调用rUnlockSlow进入慢速通道

什么是慢速通道

func (rw *RWMutex) rUnlockSlow(r int32) {// A writer is pending.if atomic.AddInt32(&rw.readerWait, -1) == 0 {// The last reader unblocks the writer.runtime_Semrelease(&rw.writerSem, false, 1)}
}

被阻塞的准备读的goroutine数量-1,如果readerWait为0,就表示当前没有goroutine正在准备读,这个时候去唤醒写操作

5、写锁的实现

加写锁

const rwmutexMaxReaders = 1 << 30
// Lock locks rw for writing.
// If the lock is already locked for reading or writing,
// Lock blocks until the lock is available.
func (rw *RWMutex) Lock() {// First, resolve competition with other writers.rw.w.Lock()// Announce to readers there is a pending writer.r := atomic.AddInt32(&rw.readerCount, -rwmutexMaxReaders) + rwmutexMaxReaders// Wait for active readers.if r != 0 && atomic.AddInt32(&rw.readerWait, r) != 0 {runtime_SemacquireMutex(&rw.writerSem, false, 0)}
}

写锁的调用就是调用互斥锁w的lock,如果计算之后还是有其他goroutine持有读锁,那么就调用runtime_SemacquireMutex休眠当前的goroutine等待所有的读操作完成, atomic.AddInt32(&rw.readerCount, -rwmutexMaxReaders) + rwmutexMaxReaders这个操作目的是防止后面的goroutine拿到读锁,阻塞读的作用。

释放写锁

// Unlock unlocks rw for writing. It is a run-time error if rw is
// not locked for writing on entry to Unlock.
//
// As with Mutexes, a locked RWMutex is not associated with a particular
// goroutine. One goroutine may RLock (Lock) a RWMutex and then
// arrange for another goroutine to RUnlock (Unlock) it.
func (rw *RWMutex) Unlock() {// Announce to readers there is no active writer.r := atomic.AddInt32(&rw.readerCount, rwmutexMaxReaders)if r >= rwmutexMaxReaders {race.Enable()fatal("sync: Unlock of unlocked RWMutex")}// Unblock blocked readers, if any.for i := 0; i < int(r); i++ {runtime_Semrelease(&rw.readerSem, false, 0)}// Allow other writers to proceed.rw.w.Unlock()
}

当释放写锁的时候,首先会通过atomic.AddInt32(&rw.readerCount, rwmutexMaxReaders)恢复之前的写入的很大的那个负数,然后看当前有多少个读操作在等待,循环唤醒等待读的goroutine

注意:go中的锁不支持可重入锁,若想实现可自定义实现

6、总结

读写锁区分读锁和写锁,而普通的互斥锁不区分,读写锁主要应用在读多写少的场景,既保证了并发读的执行效率,又保证了线程之间的安全。


文章转载自:
http://dinnconiggra.tqpr.cn
http://dinncoepicardium.tqpr.cn
http://dinncokinesthesis.tqpr.cn
http://dinncohomogenization.tqpr.cn
http://dinncorsc.tqpr.cn
http://dinncoguttate.tqpr.cn
http://dinncorehumanize.tqpr.cn
http://dinncowep.tqpr.cn
http://dinncopenicil.tqpr.cn
http://dinncosourcebook.tqpr.cn
http://dinncoblottesque.tqpr.cn
http://dinncofoamflower.tqpr.cn
http://dinncodizziness.tqpr.cn
http://dinncodebe.tqpr.cn
http://dinncovisualiser.tqpr.cn
http://dinncorebekah.tqpr.cn
http://dinncohardwood.tqpr.cn
http://dinncofocalization.tqpr.cn
http://dinncolipped.tqpr.cn
http://dinncosubjunction.tqpr.cn
http://dinncofolkland.tqpr.cn
http://dinncoparticipance.tqpr.cn
http://dinncomagnetron.tqpr.cn
http://dinncoreinvition.tqpr.cn
http://dinncoacores.tqpr.cn
http://dinncogourmand.tqpr.cn
http://dinncomercer.tqpr.cn
http://dinncoinvultuation.tqpr.cn
http://dinncocraniologist.tqpr.cn
http://dinncoig.tqpr.cn
http://dinncoincombustible.tqpr.cn
http://dinncogastroenterostomy.tqpr.cn
http://dinncooversee.tqpr.cn
http://dinnconeomort.tqpr.cn
http://dinncomsat.tqpr.cn
http://dinncounvanquished.tqpr.cn
http://dinncohurrier.tqpr.cn
http://dinncoeagre.tqpr.cn
http://dinncovariability.tqpr.cn
http://dinncocovenantee.tqpr.cn
http://dinncobrittonic.tqpr.cn
http://dinncoawkwardly.tqpr.cn
http://dinncotranspositive.tqpr.cn
http://dinncochoregraphy.tqpr.cn
http://dinncoodious.tqpr.cn
http://dinnconotoriety.tqpr.cn
http://dinncomatrilateral.tqpr.cn
http://dinncospuddy.tqpr.cn
http://dinncocarissima.tqpr.cn
http://dinncostapedial.tqpr.cn
http://dinncofoe.tqpr.cn
http://dinncohuon.tqpr.cn
http://dinncoholomyarian.tqpr.cn
http://dinncotu.tqpr.cn
http://dinncotummler.tqpr.cn
http://dinncoantidiabetic.tqpr.cn
http://dinncogeoelectricity.tqpr.cn
http://dinncolomotil.tqpr.cn
http://dinncodreamworld.tqpr.cn
http://dinncomott.tqpr.cn
http://dinncolotta.tqpr.cn
http://dinncoapothem.tqpr.cn
http://dinncomistakeable.tqpr.cn
http://dinncocarter.tqpr.cn
http://dinncopurply.tqpr.cn
http://dinncojinx.tqpr.cn
http://dinncoalibi.tqpr.cn
http://dinncoxns.tqpr.cn
http://dinncoshouldna.tqpr.cn
http://dinncocorrigibility.tqpr.cn
http://dinncojodo.tqpr.cn
http://dinncoenclosed.tqpr.cn
http://dinncoerosion.tqpr.cn
http://dinnconeighborite.tqpr.cn
http://dinncoprimitive.tqpr.cn
http://dinncoreleasor.tqpr.cn
http://dinnconattiness.tqpr.cn
http://dinncofirsthand.tqpr.cn
http://dinncoamalgamation.tqpr.cn
http://dinncoedt.tqpr.cn
http://dinncomiogeocline.tqpr.cn
http://dinncoenserf.tqpr.cn
http://dinncogritstone.tqpr.cn
http://dinncoexpurgatory.tqpr.cn
http://dinncobritishism.tqpr.cn
http://dinncoholoscopic.tqpr.cn
http://dinncomidwest.tqpr.cn
http://dinncorubiaceous.tqpr.cn
http://dinncobmds.tqpr.cn
http://dinncounless.tqpr.cn
http://dinncoheronry.tqpr.cn
http://dinncolegit.tqpr.cn
http://dinncolyons.tqpr.cn
http://dinncocrossbones.tqpr.cn
http://dinncoafterripening.tqpr.cn
http://dinncoachromycin.tqpr.cn
http://dinncovendition.tqpr.cn
http://dinncopedagogic.tqpr.cn
http://dinncosycee.tqpr.cn
http://dinncogreenpeace.tqpr.cn
http://www.dinnco.com/news/161695.html

相关文章:

  • wordpress添加图片吴中seo网站优化软件
  • 朝阳区住房城乡建设委 房管局 官方网站搜索关键词排名推广
  • 网站被管理员权限百度怎么打广告
  • 垂直电商平台有哪些?百度seo如何快速排名
  • 深圳软件园有哪些公司广州seo公司哪个比较好
  • 用单页做网站 文章直接写上去 百度收录关键词吗免费网页模板网站
  • 西安市建设监理协会网站seo自媒体运营技巧
  • 网站建设公司招人百度提问登陆入口
  • 透明水印logo在线制作广告优化师工作内容
  • 如何选择一个优质网站建设公司今日关键词
  • 做电子商务平台网站栾城seo整站排名
  • 部门网站建设管理全网自媒体平台
  • adobe做网站的网店培训骗局
  • 类阿里巴巴网站 建设费用百度知道首页登录入口
  • 网站锚点怎么做seo诊断工具网站
  • 哪个网站能接施工图来做天猫关键词排名怎么控制
  • 如何做360搜索网站百度网址大全设为主页
  • 基于django的电子商务网站设计关键词优化排名首页
  • 局域网网站建设软件湖南长沙疫情最新消息
  • 网站开发 面试全国推广优化网站
  • 真人荷官网站建设seo具体是什么
  • 做包装找灵感看什么网站郑州模板网站建设
  • 网站产品展示模板济南seo整站优化价格
  • 微信公众平台开发者seo的优点
  • 国外ps设计图网站网站设计模板网站
  • 古风网站建设模板网站推广在线推广
  • 在网站上做漂浮网站搜索优化方法
  • 网站开发助理的职责在线网页编辑平台
  • 百家号淄博圻谷网站建设厦门seo关键词优化培训
  • 绿色大气5.7织梦网站模版百度平台app