Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement go RWMutex

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to achieve go RWMutex". In daily operation, I believe many people have doubts about how to achieve go RWMutex. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to achieve go RWMutex"! Next, please follow the editor to study!

Overview

The rwlock in go is write preferred, which can avoid write lock hunger.

Read locks and write locks hold locks on a first-come-first-served basis. once a co-program holds a write lock, the later one can only get a read lock after the write lock is released.

Similarly, once a > = 1 co-program writes to the read lock, the subsequent co-program can get the write lock only after all these read locks are released.

The structure of RWMutex

RWMutex is generally implemented through ordinary locks and condition variables.

Type RWMutex struct {w Mutex / / held if there are pending writers writerSem uint32 / / semaphore for writers to wait for completing readers readerSem uint32 / / semaphore for readers to wait for completing writers readerCount int32 / / number of pending readers readerWait int32 / / number of departing readers} Lockfunc (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)}} Unlockconst rwmutexMaxReaders = 1 and will not be blocked.

Then the for loop is executed twice, waking up both co-program R1 and co-program R2.

Q7: when the write lock is taken, there are two collaborators to grab the read lock and the write lock, respectively. When the write lock is released, who will win? Func (rw * RWMutex) Unlock () {/ / Announce to readers there is no active writer. R: = atomic.AddInt32 (& rw.readerCount, rwmutexMaxReaders) / / Unblock blocked readers, if any. For I: = 0; I < int (r); iTunes + {runtime_Semrelease (& rw.readerSem, false, 0)} / / Allow other writers to proceed. Rw.w.Unlock ()}

Since the read lock is awakened first, and then w.Unlock () is called, it must be the read co-process that wins first!

Two points that I think are more skillfully written.

The entanglement of readerCount and rwmutexMaxReaders

Through the two operations of readerCount + rwmutexMaxReaders and readerCount-rwmutexMaxReaders, we can know whether there are cooperators waiting for / holding write locks and the number of cooperators currently waiting / holding read locks.

The entanglement of readerCount and readerWait

Assigning the value of readerCount directly to readerWait when Lock (), and waking up the write protocol when readerWait = 0 instead of readerCount = 0 can prevent the read protocol that is later reached by Lock () from being executed before the write procedure.

At this point, the study of "how to achieve go RWMutex" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report