分享
  1. 首页
  2. 文章

go 无锁队列

jinjiashan · · 4846 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

无锁队列适用场景:

两个线程之间的交互数据, 一个线程生产数据, 另外一个线程消费数据,效率高

缺点:需要使用固定分配的空间,不能动态增加/减少长度,存在空间浪费和无法扩展空间问题

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)
 }
}

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
4846 次点击
3 回复 | 直到 2020年11月16日 17:34:41
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏