In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to use Go concurrent programming sync.Cond". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Go concurrent programming sync.Cond.
Brief introduction
The purpose of the Go standard library to provide Cond primitives is to provide support for concurrency issues in wait / notification scenarios. Cond is usually applied to a set of goroutine waiting for a condition, and when the condition becomes true, one or all of the goroutine will be awakened to execute.
Cond is related to a certain condition, which requires the cooperation of a group of goroutine. When the condition is not met, all the goroutine waiting for this condition will be blocked. Only when this group of goroutine achieves this condition through cooperation, the waiting goroutine can continue.
This condition can be our custom true/false logical expression.
However, Cond is rarely used because it can be replaced by Channel and WaitGroup in most scenarios.
Detailed introduction
The following is the data structure and externally provided methods of Cond, and Cond maintains a wait queue and lock instance internally.
Type Cond struct {noCopy noCopy / / Lock L Locker / / waiting queue notify notifyList checker copyChecker} func NeWCond (l Locker) * Condfunc (c * Cond) Broadcast () func (c * Cond) Signal () func (c * Cond) Wait ()
NeWCond: the NeWCond method requires the caller to pass in a Locker interface, which is the Lock/UnLock method, so we can pass in a sync.Metex object
Signal: allows the caller to wake up a goroutine waiting for the current Cond. If there is one or more waiting goroutine in the Cond waiting queue, remove the first goroutine from the waiting queue and wake it up
Broadcast: allows the caller to wake up all goroutine waiting for the current Cond. If there is one or more waiting goroutine in the Cond waiting queue, clear all waiting goroutine and wake up all of them
Wait: puts the caller in the waiting queue of Cond and blocks it until it is removed from the waiting queue and awakened by the method of Signal or Broadcast
Case: Redis connection Pool
You can take a look at the following code, which uses Cond to implement a connection pool for Redis. The key code is that when the linked list is empty, you need to call the Wait method of Cond to block the gorutine. Then after goruntine has finished using the connection and returns the connection to the pool, it needs to notify other blocked goruntine to obtain the connection.
Package mainimport ("container/list"fmt"math/rand"sync"time") / / connection pool type Pool struct {lock sync.Mutex / / lock clients list.List / / connect cond * sync.Cond / / cond instance close bool / / whether to close} / / Redis Clienttype Client struct {id int32} / / create Redis Clientfunc NewClient () * Client {return & Client {id: rand.Int31n (100000) }} / / close Redis Clientfunc (this * Client) Close () {fmt.Printf ("Client:%d is shutting down", this.id)} / / create connection pool func NewPool (maxConnNum int) * Pool {pool: = new (Pool) pool.cond = sync.NewCond (& pool.lock) / / create connection for I: = 0 I < maxConnNum ITunes + {client: = NewClient () pool.clients.PushBack (client)} return pool} / / get a connection from the pool func (this * Pool) Pull () * Client {this.lock.Lock () defer this.lock.Unlock () / if this.close {fmt.Println ("Pool is closed") return nil} / / blocking for this.clients.Len () if the connection pool does not have a connection
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.