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

What is the purpose of the sync package

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "what is the role of sync package". In daily operation, I believe many people have doubts about what the role of sync package is. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "what is the role of sync package"! Next, please follow the small series to learn together!

sync.Once

Once is called many times but executed only once, Once has only one method, Once.Do(), pass a function to Do, this function will be called when the first execution of Once.Do(), and then execute Once.Do() will have no action, even if other functions are passed, they will not be executed. If you want to execute other functions, you need to recreate a Once object.

Once can be safely used in parallel in multiple routines.

type Once struct { m Mutex done uint32 }//all calls, last execution of method f func (o *Once) Do(f func())

sync.Mutex

Mutex locks ensure that only one routine can access an object at any one time.

The initial value of Mutex is unlocked.

Mutex is often used as an anonymous field for other structs, giving the struct Lock and Unlock methods.

Mutex can be safely used in parallel in multiple routines.

The// Locker interface wraps the basic Lock and UnLock methods for locking and unlocking. type Locker interface { Lock() Unlock() }// Lock is used to lock m, if m is already locked, Lock will be blocked until m is unlocked. func (m *Mutex) Lock()// Unlock is used to unlock m, which raises a panic if m is unlocked. func (m *Mutex) Unlock()

sync.RWMutex

RWMutex has one more "read lock" and "read unlock" than Mutex, allowing multiple routines to read an object simultaneously.

The initial value of RWMutex is unlocked.

RWMutex is usually used as an anonymous field for other structs.

Mutex can be safely used in parallel in multiple routines.

// Lock sets rw to a write-locked state, preventing other routines from reading or writing. func (rw *RWMutex) Lock()// Unlock Unlocks rw from a write lock, which raises a panic if rw is not write-locked. func (rw *RWMutex) Unlock()// RLock Sets rw to a read-locked state, preventing other routines from writing, but reading. func (rw *RWMutex) RLock()// Runlock Unlocks rw from read lock, which raises a panic if rw is not read locked. func (rw *RWMutex) RUnlock()// RLocker returns a mutex that encapsulates rw.RLock and rw.RUnlock into a Locker interface. func (rw *RWMutex) RLocker() Locker

sync.WaitGroup

WaitGroup is used to wait for the end of a set of routines.

The main routine calls Add to increment the wait count when it creates each subroutine, and Done to decrement the routine count when it ends.

After that, the main routine waits through the Wait method until the counter returns to zero.

//The counter is incremented by delta, which can be negative. func (wg *WaitGroup) Add(delta int)//counter decremented by 1 func (wg *WaitGroup) Done()//wait until counter returns to zero. If the counter is less than 0, the operation raises a panic. func (wg *WaitGroup) Wait()

sync.Cron

Conditional waiting keeps a routine waiting with Wait, a waiting routine continuing with Signal, and all waiting routines continuing with Broadcast.

The c.L. should be manually locked before the Wait and manually unlocked after the Wait. To avoid false wakeups, put Wait in a conditional loop

The official requirement reads as follows:

c.L.Lock() for ! condition() { c.Wait() } //Execute actions after conditions are met... c.L.Unlock()type Cond struct { L Locker //L should lock when "Check condition" or "Change condition." } //Create a conditional wait func NewCond(l Locker) *Cond// Broadcast Wakes up all waiting Waits. It is recommended to lock c.L when "changing conditions" and unlock it after changing them. func (c *Cond) Broadcast()// Signal Wakes up a waiting Wait. It is recommended to lock c.L when "changing conditions" and unlock it after changing. func (c *Cond) Signal()// Wait will unlock c.L and enter the waiting state. When it is awakened, it will re-lock c.L func (c *Cond) Wait(). At this point, the learning about "what is the role of sync package" is over. I hope to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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

Internet Technology

Wechat

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

12
Report