无锁队列适用场景:
两个线程之间的交互数据, 一个线程生产数据, 另外一个线程消费数据,效率高
缺点:需要使用固定分配的空间,不能动态增加/减少长度,存在空间浪费和无法扩展空间问题
package main import ( "fmt" "reflect" "strings" "time" )
type LoopQueue struct{ start int end int length int name string data []interface{} } func (this* LoopQueue)InitQueue(length int, name string)bool{ if nil == this || length <= 0{ return false } this.data = make([]interface{}, length) this.length = length this.name = name this.start = 0 this.end = 0 return true } func (this* LoopQueue)Push(data interface{})bool{ if nil == this{ panic("LoopQueue is nil") } if this.isFull(){ return false } var end int = this.getEnd() this.data[end] = data this.end = (end+1)%this.length return true } func (this* LoopQueue)Pop() (bool, interface{}) { if nil == this{ panic("LoopQueue is nil") } if this.isEmpty(){ return false, nil } var start = this.getStart() var startValue interface{} = this.data[start] this.start = (start+1) % this.length return true, startValue } func (this* LoopQueue)isEmpty()bool{ if nil == this{ panic("LoopQueue is nil") } if this.getStart() == this.getEnd(){ return true } return false } func (this* LoopQueue)isFull()bool{ if nil == this{ panic("LoopQueue is nil") } if this.getEnd() +1 == this.getStart(){ return true } return false } func (this* LoopQueue)getStart()int{ return this.start % this.length } func (this* LoopQueue)getEnd()int{ return this.end % this.length } var Q LoopQueue func Create(){ var index int = 0 for{ ret := Q.Push(index) if ret{ fmt.Println("PushOk", "index=", index) index++ }else{ fmt.Println("PushError", "index=", index) } time.Sleep(1e9) } } func Consum(){ for{ ret, data := Q.Pop() if ret{ fmt.Println("PopSucc", "data=",data) }else{ fmt.Println("PopError") } time.Sleep(1e9) } } //实现环形队列 func main(){ Q.InitQueue(10, "test") go Create() go Consum() for{ time.Sleep(1e9) } }
有疑问加站长微信联系(非本文作者)